net, http: always set accepted sockets as non-blocking #36745

pull darosior wants to merge 1 commits into bitcoin:master from darosior:2610_nonblocking_beltandsuspenders changing 2 files +16 −0
  1. darosior commented at 4:17 PM on November 3, 2026: member

    A mistake here would be critical, as it would allow a malicious peer to stall us indefinitely for free. This is not currently an issue because:

    • while on Linux accepted sockets do not inherit O_NONBLOCK, we always send() and recv() on Linux/macOS/BSD with MSG_DONTWAIT;
    • while on Windows MSG_DONTWAIT is unset, accepted sockets do inherit the non-blocking option [^1].

    Therefore, set accepted sockets (inbound P2P connections and HTTP clients) as non-blocking explicitly, like CreateSockOS() does for the sockets it creates. A belt-and-suspenders check costs virtually nothing here, and we already have similar ones for less important options (such as TCP_NODELAY, which as far as i know is inherited by accepted sockets on all our supported platforms).

    [^1]: Source is this implicit remark in the accept() doc, plus my own testing.

  2. net: always set accepted sockets as non-blocking
    Same for the HTTP server's clients sockets.
    
    On Linux, accepted sockets do not inherit O_NONBLOCK. This is not
    currently an issue, as we always send() and recv() with MSG_DONTWAIT.
    Still, a mistake here would be critical and the belt-and-suspenders
    check does not cost anything (we even have an unnecessary one for
    TCP_NODELAY, which *is* inherited on all of Linux/macOS/BSD/Windows).
    baf93e8ffd
  3. DrahtBot commented at 4:17 PM on November 3, 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
    ACK willcl-ark

    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-->

  4. darosior commented at 4:21 PM on November 3, 2026: member

    #36737 could be used to test this.

  5. in src/httpserver.cpp:878 in baf93e8ffd
     874 | @@ -875,6 +875,14 @@ void HTTPServer::NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& a
     875 |                   addr.ToStringAddrPort());
     876 |      }
     877 |  
     878 | +    // On Linux, accepted sockets do not inherit O_NONBLOCK. On other platforms they do. We
    


    willcl-ark commented at 12:52 PM on November 4, 2026:

    In baf93e8ffd8430bbee99246a0bcaf8e529290e08

    Cross-check against accept(2)

  6. willcl-ark approved
  7. willcl-ark commented at 1:20 PM on November 4, 2026: member

    ACK baf93e8ffd8430bbee99246a0bcaf8e529290e08

    Reviewed the HTTP, P2P, and I2P paths and they look OK.

    The option is set before the socket is handed to the event loop. If setting it fails, the connection is rejected, and ownership remains safe on every return path.

    This change removes reliance on inheritance (which doesn't work on Linux as explained) or per-call MSG_DONTWAIT, and is therefore a worthwhile defense for our socket threads.

    There are no tests here (nor would I expect there to be), so I had a clanker write up a small Python socket probe for Linux to verify (my) kernel's behaviour against what we are relying on here:

    1. A socket accepted from a nonblocking listener did not inherit O_NONBLOCK.
    2. recv(..., MSG_DONTWAIT) on that otherwise-blocking socket raised BlockingIOError instead of blocking.
    3. Setting O_NONBLOCK with F_GETFL/F_SETFL succeeded and the flag could be read back.
    4. After setting it, plain recv(..., 0) raised BlockingIOError instead of blocking.

    All behaved as expected.

  8. jpk68 commented at 6:44 PM on November 4, 2026: contributor

    ACK baf93e8ffd8430bbee99246a0bcaf8e529290e08


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-20 15:51 UTC

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