Skip to content

Commit

Permalink
Make TCP connect handle EINTR correctly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
darthunix committed Oct 4, 2023
1 parent 42ca6e4 commit 022abe6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions library/std/src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,18 @@ where
F: FnMut() -> T,
{
loop {
match cvt(f()) {
Err(ref e) if e.is_interrupted() => {}
other => return other,
let t = f();
if t.is_minus_one() {
let e = crate::io::Error::last_os_error();
if e.is_interrupted() {
continue;
}
if e.raw_os_error() == Some(libc::EISCONN) {
return Ok(t);
}
return Err(e);
}
return Ok(t);
}
}

Expand Down

0 comments on commit 022abe6

Please sign in to comment.