connect_timeout returns `Ok(())` on macOS but socket is not connected

未关闭
#652 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

  • #669 来自 @rootkiller6788 —— 已关闭,未合并

评估

难度
4/5
预计耗时
3-5 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
rust
领域
networking

调研方向

从 src/socket.rs 第 217 行附近的快速路径开始,使用报告中的 VPN 配置在 macOS 上复现 IPv6 TCP 场景。检查非阻塞 connect() 返回 Ok(()) 而不是 EINPROGRESS 时如何处理其结果,并将行为与现有的 Linux 路径进行比较。当 connect_timeout 不再报告成功,而 peer_addr() 仍返回 ENOTCONN 时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

I am observing a case on macOS where connect_timeout returns Ok(()) but the socket is not actually connected: peer_addr() (getpeername) immediately returns ENOTCONN.

Minimal reproducer:

use socket2::{Domain, Protocol, SockAddr, Socket, Type};                                                                                                                                                                                                                
use std::{net::SocketAddr, time::Duration};                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                          
fn main() {                                                                                                                                                                                                                                                             
    let remote_addr = "[<remote-ipv6>]:443"                                                                                                                                                                                                                 
        .parse::<SocketAddr>()                                                                                                                                                                                                                                          
        .unwrap();                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                          
    let socket = Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP)).unwrap();                                                                                                                                                                                 
                                                                                                                                                                                                                                                                          
    socket                                                                                                                                                                                                                                                              
        .connect_timeout(&SockAddr::from(remote_addr), Duration::from_secs(10))                                                                                                                                                                                         
        .unwrap();                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                          
    println!("connect_timeout returned Ok(())");                                                                                                                                                                                                                        
    println!("peer_addr: {:?}", socket.peer_addr());                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                                                                          

Output on the affected machine:

Socket connected
Err(Os { code: 57, kind: NotConnected, message: "Socket is not connected" })
Environment
  • macOS 15.7.3 (aarch64)
  • socket2 0.6.3
  • IPv6 TCP socket connecting to a remote server
  • The machine has a global IPv6 address and a default IPv6 route via the physical interface
  • A VPN is active that captures IPv4 traffic (via a utun interface) but does not carry IPv6

In that particular environment, decomposing Socket::connect_timeout step by step the issue is in the fast path (https://github.com/rust-lang/socket2/blob/master/src/socket.rs#L217). The non-blocking connect() call returns Ok(0) instead of Err(EINPROGRESS):

set_nonblocking(true)                                                                                                                                                                                                                                                   
connect()  → Ok(())     ← should be Err(EINPROGRESS) for a remote TCP address                                                                                                                                                                                           
set_nonblocking(false)                                                                                                                                                                                                                                                  
// connect_timeout returns Ok(()) here, poll_connect is never called                                                                                                                                                                                                    

This happens consistently (10/10 attempts) on this machine when the VPN is active.

The same network configuration on Linux works correctly: connect() returns EINPROGRESS in both cases (with or without VPN)

There's something I am missing about how connect_timeout should be used?

主要语言
Rust
星标
885
派生
328
平均合并
6 天 12 小时
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/socket2 的其他 Issue

查看 rust-lang/socket2 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。