sock: align exception monitoring between poll() and select() backends #36665

Manually merged
darosior merged 1 commit from 2610_sock_select_poll into master 2026-10-26 16:03:17 +00:00 AGit
darosior commented 2026-10-26 13:59:08 +00:00 (Migrated from github.com)

On non-Linux platforms, WaitMany monitors every socket for exceptional conditions, even when we are not looking to read from it. On Linux, it never monitors those.

Monitoring for certain socket events on some platforms but not others is inconsistent and surprising. In this instance it probably means these cases receive less testing as well, since they are ignored on our most well-tested platform.

Fix this issue by only monitoring for exceptional conditions (which is necessary to detect failed connection attempts early on Windows) for sockets we are also waiting to read from.

On non-Linux platforms, `WaitMany` monitors every socket for exceptional conditions, even when we are not looking to read from it. On Linux, it never monitors those. Monitoring for certain socket events on some platforms but not others is inconsistent and surprising. In this instance it probably means these cases receive less testing as well, since they are ignored on our most well-tested platform. Fix this issue by only monitoring for exceptional conditions (which is necessary to detect failed connection attempts early on Windows) for sockets we are also waiting to read from.
DrahtBot commented 2026-10-26 13:59:18 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK vasild, sedited, instagibbs, pinheadmz

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36665 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36665.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36665#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [vasild](https://github.com/bitcoin/bitcoin/pull/36665#pullrequestreview-5653959506), [sedited](https://github.com/bitcoin/bitcoin/pull/36665#pullrequestreview-5654409972), [instagibbs](https://github.com/bitcoin/bitcoin/pull/36665#pullrequestreview-5654759496), [pinheadmz](https://github.com/bitcoin/bitcoin/pull/36665#pullrequestreview-5654960737) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: dd9d2b919d6a97907417bf9c4f539f182f04cc01
Head: 1e0c407377034e9cbb4d52f1f534c06cdf202a36

Design and approach

Clarify whether both backends should return matching ErrorEvent results (src/util/sock.cpp:219, head)

The poll path still maps POLLERR and POLLHUP to ErrorEvent without checking requested events; Linux poll can report these conditions even when they were not requested (man7.org). So this change aligns select's exception-set registration but does not align the returned ErrorEvent behavior for send-only or zero-interest entries. This difference reaches an HTTPServer path: buffered clients can be registered with event 0, and SocketHandlerConnected calls Receive when ErrorEvent is set. Please apply a consistent requested-event policy to both backends, or narrow and document the change as select-set alignment rather than caller-visible event alignment.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4c492902f6fea4a58817c95cbe5fb719fecdcac9a41632149276919f687c2992:283fe7d72e2d0733dc3ef17f319aaa3c0a25e70562cdebdc5e12bfea8f38de15 --> Base: `dd9d2b919d6a97907417bf9c4f539f182f04cc01` Head: `1e0c407377034e9cbb4d52f1f534c06cdf202a36` ##### Design and approach **Clarify whether both backends should return matching ErrorEvent results** (src/util/sock.cpp:219, head) The poll path still maps POLLERR and POLLHUP to ErrorEvent without checking requested events; Linux poll can report these conditions even when they were not requested ([man7.org](https://man7.org/linux/man-pages/man3/poll.3p.html)). So this change aligns select's exception-set registration but does not align the returned ErrorEvent behavior for send-only or zero-interest entries. This difference reaches an HTTPServer path: buffered clients can be registered with event 0, and SocketHandlerConnected calls Receive when ErrorEvent is set. Please apply a consistent requested-event policy to both backends, or narrow and document the change as select-set alignment rather than caller-visible event alignment. [Full review report](<https://review.fish.foo/traces/36665-1e0c407377034e9cbb4d52f1f534c06cdf202a36-1374-2.html>)
vasild (Migrated from github.com) approved these changes 2026-10-26 15:08:31 +00:00
vasild (Migrated from github.com) left a comment

ACK 1e0c407377

Indeed the poll(2) and select(2) branches seem to be inconsistent.

Thanks!

ACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36 Indeed the `poll(2)` and `select(2)` branches seem to be inconsistent. Thanks!
vasild (Migrated from github.com) commented 2026-10-26 14:50:28 +00:00

Just a note to other reviewers: it looks fine to just drop FD_SET(s, &err); instead of moving it to under if (events.requested & RecvEvent). We do not expect and do not care about any "exceptional conditions":

from select(2):

exceptfds
       The  file  descriptors  in this set are watched for "exceptional conditions".  For examples of some excep‐
       tional conditions, see the discussion of POLLPRI in poll(2).

from poll(2):

 POLLPRI
       There is some exceptional condition on the file descriptor.  Possibilities include:
       •  There is out-of-band data on a TCP socket (see tcp(7)).
       •  A pseudoterminal master in packet mode has seen a state change on the slave (see ioctl_tty(2)).
       •  A cgroup.events file has been modified (see cgroups(7)).

Also, our poll() branch does not do POLLPRI.

However! https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select:

exceptfds:
If processing a connect call (nonblocking), connection attempt failed.
OOB data is available for reading (only if SO_OOBINLINE is disabled).

So, FD_SET(s, &err); is needed on Windows to handle our non-blocking connect()s.

Just a note to other reviewers: it looks fine to just drop `FD_SET(s, &err);` instead of moving it to under `if (events.requested & RecvEvent)`. We do not expect and do not care about any "exceptional conditions": from `select(2)`: > exceptfds > The file descriptors in this set are watched for "exceptional conditions". For examples of some excep‐ > tional conditions, see the discussion of POLLPRI in poll(2). from `poll(2)`: > POLLPRI > There is some exceptional condition on the file descriptor. Possibilities include: > • There is out-of-band data on a TCP socket (see tcp(7)). > • A pseudoterminal master in packet mode has seen a state change on the slave (see ioctl_tty(2)). > • A cgroup.events file has been modified (see cgroups(7)). Also, our `poll()` branch does not do `POLLPRI`. However! https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select: > exceptfds: > If processing a connect call (nonblocking), connection attempt failed. > OOB data is available for reading (only if SO_OOBINLINE is disabled). So, `FD_SET(s, &err);` is needed on Windows to handle our non-blocking `connect()`s.
sedited (Migrated from github.com) approved these changes 2026-10-26 15:17:55 +00:00
sedited (Migrated from github.com) left a comment

utACK 1e0c407377

utACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36
instagibbs (Migrated from github.com) approved these changes 2026-10-26 15:41:50 +00:00
instagibbs (Migrated from github.com) left a comment

ACK 1e0c407377

Validated a prefinal form in freebsd

ACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36 Validated a prefinal form in freebsd
pinheadmz (Migrated from github.com) approved these changes 2026-10-26 15:55:08 +00:00
pinheadmz (Migrated from github.com) left a comment

ACK 1e0c407377

built and tested on macos/arm64, clauded up a test that send an "exceptional" byte on master and compared to branch. Came to same conclusions as vasild in https://github.com/bitcoin/bitcoin/pull/36665/changes#r4378669650

This is a clean solution to the discrepency

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrfd18bFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6ZDoP/2oRg7qiKLHVahwfPfyd
TktDRlqitdF0hhqRtUPwTFJ5M2X14pM5bSEKf67B+FprjdSDfAGLmwm59QCsAyMJ
FB/8+9KvGuYKt4V3S18LjNskPTzS9sjx/sb2QJsjNFnvl366XhYQnKe9awA/JHsg
FPg1slp035Z0+qikkwvqtPMIGi+ZGwh0+RAAn46rvjIb0NP2KF4tpTnmfwPr87QK
vlSCDm7bBN1OpnAy1dc0SqUATl3g12YJiSNQdOCsN9clJv/P3c9Fb3yjg3uNdY2B
N0+y/NGyYWI6XNimIK9DDaz9kzQCjd2P4XniUm074VHFZODuA1CsyZsuPr7oebkO
XFvBr4Y72ErrlCmpuquM1rtgbkIXrw4mKwc+W0YOmEYTPx1TxT91mtkV/Za/zTk3
XCpPl4sCCeE2RGVDdvNgijIcjvNT9lnZIktM8cqMkP7vPapWoUslQ/1iOb/2PPGM
hMWJ77+Htlpu1FNL7/HKhbkEsaUOY0a8r+wgqenhjHV1hihi+M0TbNdLrj0sQxz6
IQv3JWk6vq1aA32y+aPahM6v1EJl7qLcjkTMaQtMzM13KW9fakPZeyzqtXP0MLAX
D74B1n2M+f205gx+Kg6xZg0xxNsrEbKbG743yZn6U6axMZoP98zmiX37A0iDPpaM
RjTHsecfylVeKOEfff91uw2J
=/uZl
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

ACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36 built and tested on macos/arm64, clauded up a test that send an "exceptional" byte on master and compared to branch. Came to same conclusions as vasild in https://github.com/bitcoin/bitcoin/pull/36665/changes#r4378669650 This is a clean solution to the discrepency <details><summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36 -----BEGIN PGP SIGNATURE----- iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrfd18bFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6ZDoP/2oRg7qiKLHVahwfPfyd TktDRlqitdF0hhqRtUPwTFJ5M2X14pM5bSEKf67B+FprjdSDfAGLmwm59QCsAyMJ FB/8+9KvGuYKt4V3S18LjNskPTzS9sjx/sb2QJsjNFnvl366XhYQnKe9awA/JHsg FPg1slp035Z0+qikkwvqtPMIGi+ZGwh0+RAAn46rvjIb0NP2KF4tpTnmfwPr87QK vlSCDm7bBN1OpnAy1dc0SqUATl3g12YJiSNQdOCsN9clJv/P3c9Fb3yjg3uNdY2B N0+y/NGyYWI6XNimIK9DDaz9kzQCjd2P4XniUm074VHFZODuA1CsyZsuPr7oebkO XFvBr4Y72ErrlCmpuquM1rtgbkIXrw4mKwc+W0YOmEYTPx1TxT91mtkV/Za/zTk3 XCpPl4sCCeE2RGVDdvNgijIcjvNT9lnZIktM8cqMkP7vPapWoUslQ/1iOb/2PPGM hMWJ77+Htlpu1FNL7/HKhbkEsaUOY0a8r+wgqenhjHV1hihi+M0TbNdLrj0sQxz6 IQv3JWk6vq1aA32y+aPahM6v1EJl7qLcjkTMaQtMzM13KW9fakPZeyzqtXP0MLAX D74B1n2M+f205gx+Kg6xZg0xxNsrEbKbG743yZn6U6axMZoP98zmiX37A0iDPpaM RjTHsecfylVeKOEfff91uw2J =/uZl -----END PGP SIGNATURE----- ``` pinheadmz's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/E61773CD6E01040E2F1BD78CE7E2984B6289C93A) </details>
fanquake commented 2026-10-26 16:08:31 +00:00 (Migrated from github.com)

Backported to 32.x in #36658.

Backported to `32.x` in #36658.
fanquake commented 2026-10-26 16:17:24 +00:00 (Migrated from github.com)

Backported to 31.x in #36661.

Backported to `31.x` in #36661.
fanquake commented 2026-10-26 16:25:36 +00:00 (Migrated from github.com)

Backported to 30.x in #36664.

Backported to `30.x` in #36664.
fanquake commented 2026-10-26 16:25:51 +00:00 (Migrated from github.com)

Backported to 29.x in #36666.

Backported to `29.x` in #36666.
Crypt-iQ commented 2026-10-26 17:12:15 +00:00 (Migrated from github.com)

post-merge crACK 1e0c407377

post-merge crACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36
winterrdog (Migrated from github.com) reviewed 2026-10-26 20:21:49 +00:00
winterrdog (Migrated from github.com) left a comment

post-merge ACK 1e0c407377

post-merge ACK 1e0c407377034e9cbb4d52f1f534c06cdf202a36
Sjors commented 2026-10-29 12:42:20 +00:00 (Migrated from github.com)
Post-merge ACK The underlying issue has been disclosed in https://bitcoincore.org/en/2026/10/28/disclose-tcp-oom/ I have some potential followups in [2026/10/sock-error-handler]( https://github.com/Sjors/bitcoin/commits/2026/10/sock-error-handler/) (an earlier version also did something similar to https://github.com/bitcoin/bitcoin/pull/36676) See also https://github.com/bitcoin/bitcoin/pull/36688
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36665
No description provided.