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

Aperta
#652 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #669 di @rootkiller6788 — chiusa senza merge

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
rust
Ambito
networking

Direzione di ricerca

Inizia da src/socket.rs, nel fast path intorno alla riga 217, e riproduci il caso TCP IPv6 su macOS con la configurazione VPN segnalata. Verifica come vengono gestiti i risultati di connect() non bloccante quando restituisce Ok(()) invece di EINPROGRESS e confronta il comportamento con il percorso Linux esistente. Il lavoro è completato quando connect_timeout non segnala il successo mentre peer_addr() restituisce ancora ENOTCONN.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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?

Lingua principale
Rust
Stelle
885
Fork
328
Merge medio
6g 12h
PR unite (30g)
2

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/socket2

Tutte le issue di rust-lang/socket2

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.