net: Use exclusive P2P listen sockets on Windows #36305

pull winterrdog wants to merge 2 commits into bitcoin:master from winterrdog:fix/p2p-exclusive-bind-win32 changing 2 files +20 −0
  1. winterrdog commented at 10:11 PM on September 20, 2026: contributor

    on Windows, SO_REUSEADDR does not carry over the same semantics as on POSIX systems. a second socket can set SO_REUSEADDR and bind to a port that is already in use by another process running under the same user account. this means another local process could bind to the P2P listen port and receive incoming connections intended for bitcoind.

    this matters because the P2P listener is expected to have exclusive ownership of its address and port. allowing another process to bind to it could let that process intercept incoming P2P connections. this is in a similar spirit to #36169, which made the RPC listener exclusive on Windows.

    to fix this, we use SO_EXCLUSIVEADDRUSE for the P2P listen socket on Windows instead of SO_REUSEADDR, while retaining SO_REUSEADDR on other platforms. the added test verifies the expected behaviour by attempting to bind a second socket to the same port. on Windows, this fails with WSAEACCES (10013), which Python exposes as PermissionError (an OSError subclass).

    references:

  2. test: check P2P listen port exclusivity
    Check that another socket cannot bind to a P2P port while `bitcoind` is
    already listening on it. This makes sure the listen port cannot be
    claimed by another process.
    bac33c8cab
  3. net: use exclusive P2P listen sockets on Windows
    On Windows, `SO_REUSEADDR` does not give sockets exclusive use of an
    address and port, so another process running under the same user
    account can bind to the same port and intercept incoming P2P
    connections.
    
    Make the P2P listen port exclusive on Windows by using
    `SO_EXCLUSIVEADDRUSE` so as to prevent this.
    c6971fd610
  4. DrahtBot added the label P2P on Sep 20, 2026
  5. DrahtBot commented at 10:12 PM on September 20, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36305.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  6. winterrdog commented at 10:12 PM on September 20, 2026: contributor

    indeed, this difference in SO_REUSEADDR behaviour across operating systems is somewhat surprising, and has sparked similar discussions in Tornado, CPython, and Trio.

    discussions about this similar issue elsewhere:

  7. maflcko added the label Windows on Sep 21, 2026

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-09-21 09:51 UTC

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