From dfadd177a9738de289a3a6d4b39c08d94f384c1a Mon Sep 17 00:00:00 2001 From: Denis Smirnov Date: Thu, 5 Oct 2023 16:04:28 +0700 Subject: Make TCP connect() handle EINTR correctly According to the POSIX standard, if connect() is interrupted by a signal that is caught while blocked waiting to establish a connection, connect() shall fail and set errno to EINTR, but the connection request shall not be aborted, and the connection shall be established asynchronously. If asynchronous connection was successfully established after EINTR and before the next connection attempt, OS returns EISCONN that was handled as an error before. This behavior is fixed now and we handle it as success. The problem affects MacOS users: Linux doesn't return EISCONN in this case, Windows connect() can not be interrupted without an old-fashoin WSACancelBlockingCall function that is not used in the library. So current solution gives connect() as OS specific implementation. --- library/std/src/sys_common/net.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'library/std/src/sys_common/net.rs') diff --git a/library/std/src/sys_common/net.rs b/library/std/src/sys_common/net.rs index 4f5b17deaa2..8712bd2eca7 100644 --- a/library/std/src/sys_common/net.rs +++ b/library/std/src/sys_common/net.rs @@ -226,9 +226,7 @@ impl TcpStream { init(); let sock = Socket::new(addr, c::SOCK_STREAM)?; - - let (addr, len) = addr.into_inner(); - cvt_r(|| unsafe { c::connect(sock.as_raw(), addr.as_ptr(), len) })?; + sock.connect(addr)?; Ok(TcpStream { inner: sock }) } -- cgit 1.4.1-3-g733a5