Skip to content

Commit 022abe6

Browse files
committed
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. When connect() is called one more time to handle EINTR error, it can already be established asynchronously and errno would be set to EISCONN. This case should be handled and not produce an error.
1 parent 42ca6e4 commit 022abe6

File tree

1 file changed

+11
-3
lines changed
  • library/std/src/sys/unix

1 file changed

+11
-3
lines changed

library/std/src/sys/unix/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,18 @@ where
323323
F: FnMut() -> T,
324324
{
325325
loop {
326-
match cvt(f()) {
327-
Err(ref e) if e.is_interrupted() => {}
328-
other => return other,
326+
let t = f();
327+
if t.is_minus_one() {
328+
let e = crate::io::Error::last_os_error();
329+
if e.is_interrupted() {
330+
continue;
331+
}
332+
if e.raw_os_error() == Some(libc::EISCONN) {
333+
return Ok(t);
334+
}
335+
return Err(e);
329336
}
337+
return Ok(t);
330338
}
331339
}
332340

0 commit comments

Comments
 (0)