mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:50:37 +00:00
net: Use exclusive P2P listen sockets on Windows #36305
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36305
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/p2p-exclusive-bind-win32"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
on Windows,
SO_REUSEADDRdoes not carry over the same semantics as onPOSIX systems. a second socket can set
SO_REUSEADDRand bind to aport 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_EXCLUSIVEADDRUSEfor the P2P listen socket onWindows instead of
SO_REUSEADDR, while retainingSO_REUSEADDRonother 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 asPermissionError(anOSErrorsubclass).references:
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.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.indeed, this difference in
SO_REUSEADDRbehaviour across operating systems issomewhat surprising, and has sparked similar discussions in Tornado,
CPython, and Trio.
ACK
db31a3ad1fBase:
aa76bd266df9742a266d52e0e5da8dbf033658f6Head:
db31a3ad1f75538d07707d373adbfbe90ba8e328Previously reported concerns remain unresolved: 3 suggestion.
Previous findings
[Suggestion · not confirmed] Test and document the Windows restart tradeoff
Windows documents that an exclusive listener may not be rebound while a connection it accepted remains active after close. A peer advertising a zero-size window while data remains unacknowledged can prolong that state. Bitcoin accepts untrusted inbound peers, sends them P2P traffic, and closes their sockets during shutdown without waiting for a drain. Because startup fails when the new exclusive bind fails, a quick restart after such a connection may fail where the previous Windows
SO_REUSEADDRbehavior allowed rebinding. Add a Windows test for immediate restart after an active inbound peer and explain whether this tradeoff is acceptable. If immediate restart is required, use a bounded strategy that preserves exclusive ownership. The behavior is documented, but no native Windows run establishes how often or how long it blocks rebinding. citeturn0view1Location:
src/net.cpp:3400(head).Not confirmed against this revision. First reported at
c6971fd61036cf738606afa62826af47e4315730. Last checked atdb31a3ad1f75538d07707d373adbfbe90ba8e328. The head still uses SO_EXCLUSIVEADDRUSE on Windows, and StopNodes still closes peer sockets with a plain reset before clearing the listeners. Microsoft documents that a zero-window peer can block an exclusive rebind, but Trio's empirical Windows testing contradicts this, and the merged RPC listener has the same exposure. Without a Windows run this can be neither confirmed nor ruled out.[Suggestion · not confirmed] Test and document the Windows restart tradeoff
Windows documents that an exclusive listener may not be rebound while a connection it accepted remains active after close. A peer advertising a zero-size window while data remains unacknowledged can prolong that state. Bitcoin accepts untrusted inbound peers, sends them P2P traffic, and closes their sockets during shutdown without waiting for a drain. Because startup fails when the new exclusive bind fails, a quick restart after such a connection may fail where the previous Windows
SO_REUSEADDRbehavior allowed rebinding. Add a Windows test for immediate restart after an active inbound peer and explain whether this tradeoff is acceptable. If immediate restart is required, use a bounded strategy that preserves exclusive ownership. The behavior is documented, but no native Windows run establishes how often or how long it blocks rebinding. citeturn0view1Location:
src/net.cpp:3400(head).Not confirmed against this revision. First reported at
36505a3992f509510250b9701b617a35594dbff3. Last checked atdb31a3ad1f75538d07707d373adbfbe90ba8e328. This record is based on TIME_WAIT, which is a different mechanism from the active zero-window connection in 1201:2:finding:2. Microsoft's own documentation counts only ESTABLISHED, FIN_WAIT, FIN_WAIT_2 and LAST_ACK as active, which argues against the premise. The checkout cannot establish the OS behavior.[Suggestion · still present] Clarify that the competing-bind assertion tests Windows behavior
This assertion also runs on POSIX, where the competing bind is rejected because bitcoind already listens on the wildcard endpoint; it does not exercise the Windows-only
SO_EXCLUSIVEADDRUSEbranch. Clarify in the comment that the assertion describes Windows reuse behavior, so a passing non-Windows run is not mistaken for coverage of the new option.Location:
test/functional/feature_port.py:41(head).First reported at
36505a3992f509510250b9701b617a35594dbff3. Last checked atdb31a3ad1f75538d07707d373adbfbe90ba8e328.Resolved findings
[Major · resolved] Fail the bind if exclusive-use setup fails
If
SetSockOptreturnsSOCKET_ERROR, this branch only logs the failure;BindListenPortthen continues throughBindandListenand can register the listener. Without confirmed exclusivity, a same-accountSO_REUSEADDRsocket can bind a specific local address after a default wildcard bind on supported Windows, leaving packet ownership indeterminate. (learn.microsoft.com) Please fail this bind, as the Windows HTTP listener does for the same option. No supported Windows configuration causing this option call to fail was identified, so production impact is unconfirmed.Location:
src/net.cpp:3401(head).Previous reports of the same concern:
c6971fd61036cf738606afa62826af47e4315730. Last checked atdb31a3ad1f75538d07707d373adbfbe90ba8e328. At head, a failure to set SO_EXCLUSIVEADDRUSE now sets strError, logs it as an error, and returns false before Bind/Listen. This matches httpserver.cpp's handling of the same failure.c6971fd61036cf738606afa62826af47e4315730. Last checked atdb31a3ad1f75538d07707d373adbfbe90ba8e328. This is the same mechanism as 920:1:finding:1: the bind continued after setting the exclusive option failed. The head now returns false before Bind.Review coverage was incomplete.
Review limitations
Review tier: sensitive
Full review report
Concept ACK
c6971fd610Not sure why setting the option would fail, but I think we should treat it as a fatal error, same as in httpserver.cpp.
https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse state:
Could also add a test for when the socket is bound before bitcoind:
(Edit: Realized the
setsockopt()on the python side is probably not necessary).correct, this is much better. fixed and thanks a lot!
i agreee, added the test
yes, we can leave out the Python-side
setsockopt()because on Linux (and likely other POSIX systems), settingSO_REUSEADDRon both sockets would letbitcoindbind successfully (the Python socket is only bound but does not listen), making the test fail. leaving it out is fine on Windows, since the first check (wherebitcoindbinds first) already catches that regressionACK
36505a3992Ran functional test on Windows. Thanks for taking my suggestions!
Concept ACK.
I think the commits should be swapped. Otherwise, the first commit fails the test on Windows.
done
re-ACK
db31a3ad1fThink I've seen this recently in another PR, but just thought "well, the ancestor commits CI doesn't run Windows anyway", but I agree it's more correct to swap the order.
ACK
db31a3ad1f.