Skip to content

std: check SO_ERROR after poll in TcpStream::connect_timeout - #163805

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
valentynkit:tcp-connect-timeout
Oct 26, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
valentynkit:tcp-connect-timeout

Conversation

@valentynkit

@valentynkit valentynkit commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

In TcpStream::connect_timeout after poll returned, the code treated any result without POLLERR/POLLHUP as success. That's how Linux reports a failed connect, but POSIX only specifies that the socket becomes writable once the connection is established, not how a failed attempt is reported. So on illumos connect_timeout returned Ok for a socket that never connected.

This keeps the behavior for Linux and Android, which can rely on the status from POLLERR/POLLHUP as a fast path. Other cases now check self.take_error()? to get the actual status.

connect_timeout_error test was tightened to expect an error.

Alternative: the code could be simplified by removing Linux and Android special handling, and just use self.take_error()? to check if it succeeded, but it introduces an additional syscall on the Linux happy path, when it could just check revents.

Fixes #151513

r? libs

After poll returned, the code treated any result without
`POLLERR`/`POLLHUP` as success. That's how Linux reports a failed
connect, but POSIX only specifies that the socket becomes writable once
the connection is established, not how a failed attempt is reported.
So on `illumos`, where a refused connect only sets `POLLOUT`,
`connect_timeout` returned `Ok` for a socket that never connected.

This keeps the behavior for `Linux` and `Android`, which can
rely on the status from `POLLERR`/`POLLHUP` as a fast path. Other cases
now check `self.take_error()?` to get the actual status.

`connect_timeout_error` test was tightened to expect an error.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 5, 2026
Comment on lines +227 to 233
let failure_shows_in_revents =
cfg!(any(target_os = "linux", target_os = "android"));
let hup_or_err = pollfd.revents & (libc::POLLHUP | libc::POLLERR) != 0;

if failure_shows_in_revents && !hup_or_err {
return Ok(());
}

@clarfonthey clarfonthey Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of soft preference for using cfg_select! here over a cfg! boolean, since it kind of makes the system-specific spot more clear:

Suggested change
let failure_shows_in_revents =
cfg!(any(target_os = "linux", target_os = "android"));
let hup_or_err = pollfd.revents & (libc::POLLHUP | libc::POLLERR) != 0;
if failure_shows_in_revents && !hup_or_err {
return Ok(());
}
let hup_or_err = pollfd.revents & (libc::POLLHUP | libc::POLLERR) != 0;
cfg_select! {
any(target_os = "linux", target_os = "android") => {
if !hup_or_err {
return Ok(());
}
}
}

Although I guess the flow is still a bit strange since I'm not sure what exact case the final if hup_or_err should qualify for.

View changes since the review

@clarfonthey

Copy link
Copy Markdown
Contributor

@rustbot reroll

@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 6114124 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

Reason for tree closure: test-aarch64-apple-macos-26-2 out of disk space

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 25, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 25, 2026
…=Mark-Simulacrum

std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`

In `TcpStream::connect_timeout` after poll returned, the code treated any result without `POLLERR`/`POLLHUP` as success. That's how Linux reports a failed connect, but [POSIX](https://pubs.opengroup.org/onlinepubs/9799919799/functions/connect.html) only specifies that the socket becomes writable once the connection is established, not how a failed attempt is reported. So on `illumos` `connect_timeout` returned `Ok` for a socket that never connected.

This keeps the behavior for `Linux` and `Android`, which can rely on the status from `POLLERR`/`POLLHUP` as a fast path. Other cases now check `self.take_error()?` to get the actual status.

`connect_timeout_error` test was tightened to expect an error.

Alternative: the code could be simplified by removing `Linux` and `Android` special handling, and just use `self.take_error()?` to check if it succeeded, but it introduces an additional syscall on the Linux happy path, when it could just check `revents`.

Fixes rust-lang#151513

r? libs
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 25, 2026
…=Mark-Simulacrum

std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`

In `TcpStream::connect_timeout` after poll returned, the code treated any result without `POLLERR`/`POLLHUP` as success. That's how Linux reports a failed connect, but [POSIX](https://pubs.opengroup.org/onlinepubs/9799919799/functions/connect.html) only specifies that the socket becomes writable once the connection is established, not how a failed attempt is reported. So on `illumos` `connect_timeout` returned `Ok` for a socket that never connected.

This keeps the behavior for `Linux` and `Android`, which can rely on the status from `POLLERR`/`POLLHUP` as a fast path. Other cases now check `self.take_error()?` to get the actual status.

`connect_timeout_error` test was tightened to expect an error.

Alternative: the code could be simplified by removing `Linux` and `Android` special handling, and just use `self.take_error()?` to check if it succeeded, but it introduces an additional syscall on the Linux happy path, when it could just check `revents`.

Fixes rust-lang#151513

r? libs
rust-bors Bot pushed a commit that referenced this pull request Oct 25, 2026
…uwer

Rollup of 20 pull requests

Successful merges:

 - #163730 (add tests over re-entrancy guarantees for 160776)
 - #164834 (miri subtree update)
 - #164849 (Use the span of the doctest code block fence for figuring out the line number)
 - #164883 (rustdoc: don't panic on a nested use group with no path prefix)
 - #164890 (Store cold blocks in a bitset instead of a Vec of bools)
 - #163458 (stabilize {Rc, Arc}::is_unique)
 - #163499 (vec: stabilise allocators for into_iter)
 - #163805 (std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`)
 - #163816 (stabilize String::into_chars)
 - #163819 (Correct documentation of what `core::ops::Code` is.)
 - #163847 (add regression test for reborrow-invariant-type)
 - #164006 (Arduously fix typo)
 - #164009 (Update `Ready` future expect messages)
 - #164522 (Add IPv6 hop limit stubs to the motor OS net backend)
 - #164602 (Improve seek_read_buf_exact() test coverage to match seek_read_exact())
 - #164772 (docs: Two nitpick changes to `HashMap`, `BTreeMap` Entry API docs)
 - #164794 (Expose FloatErrorKind)
 - #164802 (Migrate stable-specific run-make tests to UI tests)
 - #164899 (Only recover if we may in `parse_expr_tuple_parens`)
 - #164903 (rustc-dev-guide subtree update)

Failed merges:

 - #164856 (Switch from `insta` to `expect-test` in bootstrap)
@rust-bors
rust-bors Bot merged commit ecedd45 into rust-lang:main Oct 26, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 26, 2026
Rollup merge of #163805 - valentynkit:tcp-connect-timeout, r=Mark-Simulacrum

std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`

In `TcpStream::connect_timeout` after poll returned, the code treated any result without `POLLERR`/`POLLHUP` as success. That's how Linux reports a failed connect, but [POSIX](https://pubs.opengroup.org/onlinepubs/9799919799/functions/connect.html) only specifies that the socket becomes writable once the connection is established, not how a failed attempt is reported. So on `illumos` `connect_timeout` returned `Ok` for a socket that never connected.

This keeps the behavior for `Linux` and `Android`, which can rely on the status from `POLLERR`/`POLLHUP` as a fast path. Other cases now check `self.take_error()?` to get the actual status.

`connect_timeout_error` test was tightened to expect an error.

Alternative: the code could be simplified by removing `Linux` and `Android` special handling, and just use `self.take_error()?` to check if it succeeded, but it introduces an additional syscall on the Linux happy path, when it could just check `revents`.

Fixes #151513

r? libs
moabo3li pushed a commit to moabo3li/miri that referenced this pull request Oct 26, 2026
…uwer

Rollup of 20 pull requests

Successful merges:

 - rust-lang/rust#163730 (add tests over re-entrancy guarantees for 160776)
 - rust-lang/rust#164834 (miri subtree update)
 - rust-lang/rust#164849 (Use the span of the doctest code block fence for figuring out the line number)
 - rust-lang/rust#164883 (rustdoc: don't panic on a nested use group with no path prefix)
 - rust-lang/rust#164890 (Store cold blocks in a bitset instead of a Vec of bools)
 - rust-lang/rust#163458 (stabilize {Rc, Arc}::is_unique)
 - rust-lang/rust#163499 (vec: stabilise allocators for into_iter)
 - rust-lang/rust#163805 (std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`)
 - rust-lang/rust#163816 (stabilize String::into_chars)
 - rust-lang/rust#163819 (Correct documentation of what `core::ops::Code` is.)
 - rust-lang/rust#163847 (add regression test for reborrow-invariant-type)
 - rust-lang/rust#164006 (Arduously fix typo)
 - rust-lang/rust#164009 (Update `Ready` future expect messages)
 - rust-lang/rust#164522 (Add IPv6 hop limit stubs to the motor OS net backend)
 - rust-lang/rust#164602 (Improve seek_read_buf_exact() test coverage to match seek_read_exact())
 - rust-lang/rust#164772 (docs: Two nitpick changes to `HashMap`, `BTreeMap` Entry API docs)
 - rust-lang/rust#164794 (Expose FloatErrorKind)
 - rust-lang/rust#164802 (Migrate stable-specific run-make tests to UI tests)
 - rust-lang/rust#164899 (Only recover if we may in `parse_expr_tuple_parens`)
 - rust-lang/rust#164903 (rustc-dev-guide subtree update)

Failed merges:

 - rust-lang/rust#164856 (Switch from `insta` to `expect-test` in bootstrap)
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Oct 30, 2026
…uwer

Rollup of 20 pull requests

Successful merges:

 - rust-lang/rust#163730 (add tests over re-entrancy guarantees for 160776)
 - rust-lang/rust#164834 (miri subtree update)
 - rust-lang/rust#164849 (Use the span of the doctest code block fence for figuring out the line number)
 - rust-lang/rust#164883 (rustdoc: don't panic on a nested use group with no path prefix)
 - rust-lang/rust#164890 (Store cold blocks in a bitset instead of a Vec of bools)
 - rust-lang/rust#163458 (stabilize {Rc, Arc}::is_unique)
 - rust-lang/rust#163499 (vec: stabilise allocators for into_iter)
 - rust-lang/rust#163805 (std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`)
 - rust-lang/rust#163816 (stabilize String::into_chars)
 - rust-lang/rust#163819 (Correct documentation of what `core::ops::Code` is.)
 - rust-lang/rust#163847 (add regression test for reborrow-invariant-type)
 - rust-lang/rust#164006 (Arduously fix typo)
 - rust-lang/rust#164009 (Update `Ready` future expect messages)
 - rust-lang/rust#164522 (Add IPv6 hop limit stubs to the motor OS net backend)
 - rust-lang/rust#164602 (Improve seek_read_buf_exact() test coverage to match seek_read_exact())
 - rust-lang/rust#164772 (docs: Two nitpick changes to `HashMap`, `BTreeMap` Entry API docs)
 - rust-lang/rust#164794 (Expose FloatErrorKind)
 - rust-lang/rust#164802 (Migrate stable-specific run-make tests to UI tests)
 - rust-lang/rust#164899 (Only recover if we may in `parse_expr_tuple_parens`)
 - rust-lang/rust#164903 (rustc-dev-guide subtree update)

Failed merges:

 - rust-lang/rust#164856 (Switch from `insta` to `expect-test` in bootstrap)
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Oct 30, 2026
…uwer

Rollup of 20 pull requests

Successful merges:

 - rust-lang/rust#163730 (add tests over re-entrancy guarantees for 160776)
 - rust-lang/rust#164834 (miri subtree update)
 - rust-lang/rust#164849 (Use the span of the doctest code block fence for figuring out the line number)
 - rust-lang/rust#164883 (rustdoc: don't panic on a nested use group with no path prefix)
 - rust-lang/rust#164890 (Store cold blocks in a bitset instead of a Vec of bools)
 - rust-lang/rust#163458 (stabilize {Rc, Arc}::is_unique)
 - rust-lang/rust#163499 (vec: stabilise allocators for into_iter)
 - rust-lang/rust#163805 (std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`)
 - rust-lang/rust#163816 (stabilize String::into_chars)
 - rust-lang/rust#163819 (Correct documentation of what `core::ops::Code` is.)
 - rust-lang/rust#163847 (add regression test for reborrow-invariant-type)
 - rust-lang/rust#164006 (Arduously fix typo)
 - rust-lang/rust#164009 (Update `Ready` future expect messages)
 - rust-lang/rust#164522 (Add IPv6 hop limit stubs to the motor OS net backend)
 - rust-lang/rust#164602 (Improve seek_read_buf_exact() test coverage to match seek_read_exact())
 - rust-lang/rust#164772 (docs: Two nitpick changes to `HashMap`, `BTreeMap` Entry API docs)
 - rust-lang/rust#164794 (Expose FloatErrorKind)
 - rust-lang/rust#164802 (Migrate stable-specific run-make tests to UI tests)
 - rust-lang/rust#164899 (Only recover if we may in `parse_expr_tuple_parens`)
 - rust-lang/rust#164903 (rustc-dev-guide subtree update)

Failed merges:

 - rust-lang/rust#164856 (Switch from `insta` to `expect-test` in bootstrap)
@clarfonthey clarfonthey added the A-networking Area: `std::net` label Nov 3, 2026
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Nov 16, 2026
…uwer

Rollup of 20 pull requests

Successful merges:

 - rust-lang/rust#163730 (add tests over re-entrancy guarantees for 160776)
 - rust-lang/rust#164834 (miri subtree update)
 - rust-lang/rust#164849 (Use the span of the doctest code block fence for figuring out the line number)
 - rust-lang/rust#164883 (rustdoc: don't panic on a nested use group with no path prefix)
 - rust-lang/rust#164890 (Store cold blocks in a bitset instead of a Vec of bools)
 - rust-lang/rust#163458 (stabilize {Rc, Arc}::is_unique)
 - rust-lang/rust#163499 (vec: stabilise allocators for into_iter)
 - rust-lang/rust#163805 (std: check `SO_ERROR` after poll in `TcpStream::connect_timeout`)
 - rust-lang/rust#163816 (stabilize String::into_chars)
 - rust-lang/rust#163819 (Correct documentation of what `core::ops::Code` is.)
 - rust-lang/rust#163847 (add regression test for reborrow-invariant-type)
 - rust-lang/rust#164006 (Arduously fix typo)
 - rust-lang/rust#164009 (Update `Ready` future expect messages)
 - rust-lang/rust#164522 (Add IPv6 hop limit stubs to the motor OS net backend)
 - rust-lang/rust#164602 (Improve seek_read_buf_exact() test coverage to match seek_read_exact())
 - rust-lang/rust#164772 (docs: Two nitpick changes to `HashMap`, `BTreeMap` Entry API docs)
 - rust-lang/rust#164794 (Expose FloatErrorKind)
 - rust-lang/rust#164802 (Migrate stable-specific run-make tests to UI tests)
 - rust-lang/rust#164899 (Only recover if we may in `parse_expr_tuple_parens`)
 - rust-lang/rust#164903 (rustc-dev-guide subtree update)

Failed merges:

 - rust-lang/rust#164856 (Switch from `insta` to `expect-test` in bootstrap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-networking Area: `std::net` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

connect_timeout incorrectly succeeds on illumos and probably other systems

5 participants