Repository navigation
std: check SO_ERROR after poll in TcpStream::connect_timeout - #163805
Merged
Merged
Conversation
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.
asder8215
reviewed
Oct 5, 2026
clarfonthey
reviewed
Oct 10, 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(()); | ||
| } |
Contributor
There was a problem hiding this comment.
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.
Contributor
|
@rustbot reroll |
Member
|
@bors r+ rollup |
Contributor
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
This was referenced Oct 25, 2026
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 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)
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
TcpStream::connect_timeoutafter poll returned, the code treated any result withoutPOLLERR/POLLHUPas 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 onillumosconnect_timeoutreturnedOkfor a socket that never connected.This keeps the behavior for
LinuxandAndroid, which can rely on the status fromPOLLERR/POLLHUPas a fast path. Other cases now checkself.take_error()?to get the actual status.connect_timeout_errortest was tightened to expect an error.Alternative: the code could be simplified by removing
LinuxandAndroidspecial handling, and just useself.take_error()?to check if it succeeded, but it introduces an additional syscall on the Linux happy path, when it could just checkrevents.Fixes #151513
r? libs