net: stop monitoring select exceptions outside Windows #36716

pull Sjors wants to merge 7 commits into bitcoin:master from Sjors:2026/10/windows-select-exceptions changing 11 files +283 −16
  1. Sjors commented at 4:13 AM on October 31, 2026: member

    Alternative approach to #36688, citing the relevant parts of its description:

    There is currently nothing preventing us to receive TCP urgent data, which would never be returned by recv(). Linux's poll() is indifferent to this, but the select() socket monitoring may keep reporting it as pending on some platforms.

    This would not prevent progress nor lead to a crash anymore after #36665, but it's still undesirable.

    Fix this by ignoring the exceptionSet. Except on Windows, where we instead disconnect if its used.

    We could instead have set SO_OOBINLINE on our sockets, so recv() returns any urgent data sent to us (and likely leads us to disconnect the peer). RFC 6093 doesn't forbid this, but the current approach seems more clear and direct to me.

    The PR makes two additional changes for clarity:

    • wrap the FD_ISSET check in #ifdef WIN32 (no behavior change)
    • instead of if (recvSet || errorSet), check errorSet separately and earlier (emits a different log message)

    Test coverage is introduced in p2p_socket_errors.py.

    Finally a similar fix is applied to HTTP.

  2. DrahtBot added the label P2P on Oct 31, 2026
  3. DrahtBot commented at 4:13 AM on October 31, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    Type Reviewers
    Concept ACK midnightmagic
    Stale ACK darosior

    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.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36744 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36744.svg"></sub> (refactor: fix mismatched and undefined declarations by fanquake)
    • #36434 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36434.svg"></sub> (http: support unix sockets by pinheadmz)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. pinheadmz commented at 11:38 AM on October 31, 2026: member

    Mine as well apply the same logic to HTTP, probably right here:

    https://github.com/bitcoin/bitcoin/blob/5669fda5ed85ac739c1678c03bd7c25193d099c6/src/httpserver.cpp#L916-L918

    I wrote functional test for the first PR, could be adapted to ensuring HTTP disconnects on urgent data:

    https://gist.github.com/pinheadmz/f5bb77865f870865e44c54d5751b569a

  5. Sjors commented at 7:42 AM on November 1, 2026: member

    My preference is to fix HTTP separately as a followup, but I'm open to doing it here.

  6. in src/util/sock.cpp:222 in 8754e5c1c4 outdated
     215 | @@ -216,7 +216,10 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per
     216 |          const auto& s = sock->m_socket;
     217 |          if (events.requested & RecvEvent) {
     218 |              FD_SET(s, &recv);
     219 | +#ifdef WIN32
     220 | +            // Windows reports a failed non-blocking connect() in the exception set.
     221 |              FD_SET(s, &err);
     222 | +#endif
    


    vasild commented at 4:05 PM on November 2, 2026:

    Some lines below is this code:

             if (FD_ISSET(s, &err)) {
                 events.occurred |= ErrorEvent;
             }       
    

    which handles the outcome of select(2). Is it true that with this PR that would be no-op on non-Windows? Because we only FD_SET(s, &err) on Windows. If yes, then consider also putting it inside #ifdef WIN32 to make that more clear.


    Sjors commented at 11:44 AM on November 4, 2026:

    Added a commit for this.

  7. in src/net.cpp:2205 in 8754e5c1c4 outdated
    2200 | @@ -2201,6 +2201,18 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
    2201 |              }
    2202 |          }
    2203 |  
    2204 | +#ifdef WIN32
    2205 | +        if (errorSet) {
    


    vasild commented at 4:09 PM on November 2, 2026:

    Should the below if (recvSet || errorSet) be revised as well? If we only set errorSet on Windows and we already handle it here, then the below can be just if (recvSet).

    Or, given that we only set errorSet on Windows, this #ifdef WIN32 here is not needed.


    Sjors commented at 1:53 PM on November 4, 2026:

    Should the below if (recvSet || errorSet) be revised as well?

    I added a commit to check errorSet separately and earlier, for poll().

    I spent some time trying to understand if that's a behavior change, but AFAIK it just emits a different log message. I added a test to demonstrate that.

  8. in src/netbase.cpp:574 in 8754e5c1c4
     569 | +        // transport treats TCP as a uniform byte stream; interpreting the
     570 | +        // cleartext URG flag as an interrupt would violate that design. P2P
     571 | +        // should never support such interrupts.
     572 | +        //
     573 | +        // RFC 6093 §6 ¶1 requires SO_OOBINLINE for applications that employ
     574 | +        // urgent indications. P2P does not, so leave SO_OOBINLINE unset.
    


    darosior commented at 11:55 PM on November 2, 2026:

    This does not follow, and regardless that seems like a confusing comment to add here.

    My reading of RFC 6093 is that it 1) discourages applications from sending urgent data 2) advises applications to set SO_OOBINLINE to fix the broken out of band mechanism regardless of their use for urgent data.

    But regardless, i think that comment just adds more confusion than it clears up, and it would be better to drop it.


    Sjors commented at 8:23 AM on November 4, 2026:
    1. discourages applications from sending urgent data

    The RFC says:

    Even though applications SHOULD NOT employ the urgent mechanism

    This isn't about a misbehaving sender, but rather about application design (both sides).

    This does not follow

    I'm not sure what exactly "this" refers to. It's true that the RFC does not forbid applications from setting SO_OOBINLINE if they don't "employ urgent indications".

    I do think it's good to document that we don't "employ urgent indications". The absence of the setting implies it, but an actual code comment is more clear.

    In any case, I'll drop the comment commit.

  9. darosior commented at 11:55 PM on November 2, 2026: member

    crACK 1fde57f1deda683129ccead9c30cfad0e235914e because it fixes the issue in P2P, but i prefer @theuni's suggestion to just always fail on error/exception during a wait (And i guess start listening for POLLPRI on Linux to consistently disconnect on URG data rather than ignoring it on one platform and disconnecting on another).

    I don't understand the point of the second commit.

  10. Sjors marked this as a draft on Nov 4, 2026
  11. sock: guard select exception result handling with WIN32
    The select() backend only adds sockets to the exception set on Windows.
    Elsewhere, the set starts empty and remains empty after select(), so
    checking it with FD_ISSET() is a no-op. Match the existing WIN32 guard
    around FD_SET().
    
    This commit does not change behavior.
    fe55ff549c
  12. test: move send-stall wait into the framework 938133d634
  13. test: cover P2P reset while sending
    Reset a slow reader with block output stalled and more input queued
    behind receive backpressure.
    13b11344e9
  14. net: disconnect on poll errors before sending
    Disconnect directly on POLLERR/POLLHUP without read readiness,
    skipping the send attempt that would otherwise fail and disconnect
    the peer. This also changes the debug log message.
    
    Only call recv() for readable sockets.
    a2d3de8d82
  15. test: cover P2P TCP urgent data handling
    Check that urgent indications do not prevent ordinary P2P input from
    being processed or count as received message bytes.
    
    Co-authored-by: Matthew Zipkin <pinheadmz@pm.me>
    fc0d67c3ae
  16. net: stop monitoring select exceptions outside Windows
    Ignore select() exceptions outside Windows. Disconnect established P2P
    peers on Windows exceptions instead of probing them with recv(). Update
    the Windows functional test to require disconnection.
    
    Co-authored-by: Antoine Poinsot <mail@antoinep.com>
    1c27aa5bef
  17. http: handle socket errors separately from reads
    Disconnect HTTP clients on Windows socket exceptions, including TCP
    urgent data. On poll errors or hangups without read readiness,
    disconnect before sending instead of attempting an unrequested read.
    
    Co-authored-by: Matthew Zipkin <pinheadmz@pm.me>
    090e0b71c0
  18. Sjors force-pushed on Nov 4, 2026
  19. Sjors commented at 3:57 PM on November 4, 2026: member

    I dropped the comment-only commit.

    Added two commits based on @vasild's feedback.

    Also added and expanded @pinheadmz's tests.

    Finally, I added the http fix at the end. It should be simple to review since it's the same approach.

    I also tested on Windows native.

  20. Sjors marked this as ready for review on Nov 4, 2026
  21. midnightmagic commented at 12:48 AM on November 6, 2026: contributor

    We could instead have set SO_OOBINLINE on our sockets, so recv() returns any urgent data sent to us (and likely leads us to disconnect the peer). RFC 6093 doesn't forbid this, but the current approach seems more clear and direct to me.

    I believe it's possible to construct an oob urgent ptr for data which will never be sent and which will then never be received but will still trigger the exception set. Setting SO_OOBINLINE thus can't ultimately clear the select() exception because no data is actually inline to read. On the BSDs I examined (and therefore likely OS/X) this will still generate an exception so the current select() call will return immediately again. As noted elsewhere, this doesn't technically prevent eventual node progress.

    fwiw, I strongly suggest that oob data be considered an error condition based on this. There is nowhere for UNIX-y OSes which an oob byte will ever be generated by a valid p2p peer, and thus it'll only ever be generated in this context — therefore disconnection is a logical and completely fair result. I personally think at this point it's worth a ban.

    (Uh, just to be clear, at the very least I think we should delete the exceptfds from the select() call entirely, so strong ACK for this approach.)

  22. Sjors commented at 8:43 AM on November 6, 2026: member

    I strongly suggest that oob data be considered an error condition based on this

    I agree that if there was a DISCONNECT_ON_OOB flag, we should set it. But we can't directly detect if such data was present. All we can know is that errorSet was triggered, which could be for unrelated OS reasons. That could cause us to disconnect a well behaved peer.

    That's why this PR simply ignores the exception set (outside Windows), instead of trying to handle it. It might be possible with poll(), but that would be better for a followup.

  23. theuni commented at 9:27 PM on November 6, 2026: member

    @midnightmagic agreed.

    Imo we should just always disconnect when an exception is set, or when oob/prio are found. I don't see any need for platform-specific behavior here. I'd rather go with: https://github.com/theuni/bitcoin/commits/net-oob-error/

    That branch unifies the select/poll behaviors, and causes a disconnect (on all platforms) if any of those conditions are true.

    It differs from what's here in that it turns POLLHUP into a read event as it should be, rather than an error. That simplifies things significantly, as it allows draining to happen naturally, and it means that the exception set is truly only comprised of exceptions.

    If receiving is off (meaning that we have data to send) and we receive a SIGHUP, we'll disconnect on the next send.

    It fixes up the tests to show that OOB data always causes a disconnect.

  24. DrahtBot added the label Needs rebase on Nov 7, 2026
  25. DrahtBot commented at 1:55 PM on November 7, 2026: contributor

    <!--cf906140f33d8803c4a75a2196329ecb-->

    🐙 This pull request conflicts with the target branch and needs rebase.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-07 16:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me