net: Use exclusive P2P listen sockets on Windows #36305

Manually merged
winterrdog merged 2 commits from fix/p2p-exclusive-bind-win32 into master 2026-11-02 14:49:18 +00:00 AGit
winterrdog commented 2026-09-20 22:11:54 +00:00 (Migrated from github.com)

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:

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: - https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse - https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ/14388707#14388707
DrahtBot commented 2026-09-20 22:12:01 +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 jpk68, hodlinator, hebasto

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/36305 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36305.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36305#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 | [jpk68](https://github.com/bitcoin/bitcoin/pull/36305#issuecomment-6281543300), [hodlinator](https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5672657235), [hebasto](https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5738344082) | 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-->
winterrdog commented 2026-09-20 22:12:42 +00:00 (Migrated from github.com)
indeed, this difference in `SO_REUSEADDR` behaviour across operating systems is somewhat surprising, and has sparked similar discussions in Tornado, CPython, and Trio. - https://github.com/python-trio/trio/issues/39 - https://github.com/tornadoweb/tornado/issues/550 - https://github.com/python/cpython/issues/46802
jpk68 commented 2026-10-20 01:58:32 +00:00 (Migrated from github.com)
ACK db31a3ad1f75538d07707d373adbfbe90ba8e328
First-time contributor

Base: aa76bd266df9742a266d52e0e5da8dbf033658f6
Head: db31a3ad1f75538d07707d373adbfbe90ba8e328

Previously 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_REUSEADDR behavior 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. citeturn0view1

Location: src/net.cpp:3400 (head).

Not confirmed against this revision. First reported at c6971fd61036cf738606afa62826af47e4315730. Last checked at db31a3ad1f75538d07707d373adbfbe90ba8e328. 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_REUSEADDR behavior 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. citeturn0view1

Location: src/net.cpp:3400 (head).

Not confirmed against this revision. First reported at 36505a3992f509510250b9701b617a35594dbff3. Last checked at db31a3ad1f75538d07707d373adbfbe90ba8e328. 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_EXCLUSIVEADDRUSE branch. 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 at db31a3ad1f75538d07707d373adbfbe90ba8e328.

Resolved findings
[Major · resolved] Fail the bind if exclusive-use setup fails

If SetSockOpt returns SOCKET_ERROR, this branch only logs the failure; BindListenPort then continues through Bind and Listen and can register the listener. Without confirmed exclusivity, a same-account SO_REUSEADDR socket 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:

  • src/net.cpp:3401. Fixed. First reported at c6971fd61036cf738606afa62826af47e4315730. Last checked at db31a3ad1f75538d07707d373adbfbe90ba8e328. 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.
  • src/net.cpp:3400. Fixed. First reported at c6971fd61036cf738606afa62826af47e4315730. Last checked at db31a3ad1f75538d07707d373adbfbe90ba8e328. 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
  • adversarial_claude: The finding depends on Windows TCP behaviour that the repository cannot show: whether connections accepted on an SO_EXCLUSIVEADDRUSE listener still block a new exclusive bind after the process exits. This covers both ordinary TIME_WAIT and the case where the peer advertises a zero window.
  • design: The checkout does not establish whether supported Windows versions allow an exclusive P2P listener to rebind while accepted peer connections from the previous run are in TIME_WAIT. That determines whether a clean restart after inbound traffic can fail until those entries expire.
  • verifier: The restart-availability candidates turn on how Windows TCP actually behaves. The checkout cannot show this, and the sources disagree. Microsoft's SO_EXCLUSIVEADDRUSE documentation says a rebind is blocked while an accepted connection is still 'active' (ESTABLISHED, FIN_WAIT, FIN_WAIT_2 or LAST_ACK), and that a zero-window peer can keep it active. Trio's issue #928, however, reports empirical Windows testing in which this caveat did not occur. Settling it needs a native Windows run: restart a listening node immediately after inbound peers were connected, including a peer that has stopped reading.
  • verifier: Unresolved candidate: All three candidates share one root cause: replacing SO_REUSEADDR with SO_EXCLUSIVEADDRUSE on Windows might stop a restarted node from rebinding while connections accepted by the old listener are still closing. The code facts are verified. The base comment at src/net.cpp:3395-3396 explains SO_REUSEADDR as allowing a rebind during TIME_WAIT. At head, Windows sets only SO_EXCLUSIVEADDRUSE (3400). CloseSocketDisconnect does a plain m_sock.reset() (562-568), and StopNodes closes peer sockets before vhListenSocket.clear() (3766-3780). A failed IPv4 wildcard bind is fatal (3565-3566, 3577-3583). The OS-level premise, however, cannot be established. Microsoft's documentation counts only ESTABLISHED, FIN_WAIT, FIN_WAIT_2 and LAST_ACK as active, so design:1's TIME_WAIT premise conflicts with the documentation itself. The zero-window case (adversarial:1, adversarial_claude:1) is documented by Microsoft, but Trio's issue #928 reports that the caveat did not hold in empirical Windows testing. The RPC listener already merged in src/httpserver.cpp:725-734 has the same exposure, and Windows functional tests restart nodes that have peers. What is still needed is a native Windows run that restarts immediately after inbound peers, including one that has stopped reading with data queued.

Review tier: sensitive

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:ab973404fb8e3c0d560a5f0df76c1f96a2e0e21182cfc208ac10a039068cb623:00633cd9cb08c8f4661c30fbf00eaba6318f037423dd36deda26cc4a9d01c2c2 --> Base: `aa76bd266df9742a266d52e0e5da8dbf033658f6` Head: `db31a3ad1f75538d07707d373adbfbe90ba8e328` <!-- ralph-case:021ec25536ffd4e216d7eeec72622028bc9ed9a23af161ab114477084106c719 --> Previously reported concerns remain unresolved: 3 suggestion. ##### Previous findings <details> <summary>[Suggestion · not confirmed] Test and document the Windows restart tradeoff</summary> 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_REUSEADDR` behavior 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. citeturn0view1 Location: `src/net.cpp:3400` (head). Not confirmed against this revision. First reported at `c6971fd61036cf738606afa62826af47e4315730`. Last checked at `db31a3ad1f75538d07707d373adbfbe90ba8e328`. 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. </details> <details> <summary>[Suggestion · not confirmed] Test and document the Windows restart tradeoff</summary> 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_REUSEADDR` behavior 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. citeturn0view1 Location: `src/net.cpp:3400` (head). Not confirmed against this revision. First reported at `36505a3992f509510250b9701b617a35594dbff3`. Last checked at `db31a3ad1f75538d07707d373adbfbe90ba8e328`. 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. </details> <details> <summary>[Suggestion · still present] Clarify that the competing-bind assertion tests Windows behavior</summary> 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_EXCLUSIVEADDRUSE` branch. 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 at `db31a3ad1f75538d07707d373adbfbe90ba8e328`. </details> <details> <summary>Resolved findings</summary> <details> <summary>[Major · resolved] Fail the bind if exclusive-use setup fails</summary> If `SetSockOpt` returns `SOCKET_ERROR`, this branch only logs the failure; `BindListenPort` then continues through `Bind` and `Listen` and can register the listener. Without confirmed exclusivity, a same-account `SO_REUSEADDR` socket can bind a specific local address after a default wildcard bind on supported Windows, leaving packet ownership indeterminate. ([learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse)) 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: - src/net.cpp:3401. Fixed. First reported at `c6971fd61036cf738606afa62826af47e4315730`. Last checked at `db31a3ad1f75538d07707d373adbfbe90ba8e328`. 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. - src/net.cpp:3400. Fixed. First reported at `c6971fd61036cf738606afa62826af47e4315730`. Last checked at `db31a3ad1f75538d07707d373adbfbe90ba8e328`. 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. </details> </details> Review coverage was incomplete. ##### Review limitations - adversarial_claude: The finding depends on Windows TCP behaviour that the repository cannot show: whether connections accepted on an SO_EXCLUSIVEADDRUSE listener still block a new exclusive bind after the process exits. This covers both ordinary TIME_WAIT and the case where the peer advertises a zero window. - design: The checkout does not establish whether supported Windows versions allow an exclusive P2P listener to rebind while accepted peer connections from the previous run are in TIME_WAIT. That determines whether a clean restart after inbound traffic can fail until those entries expire. - verifier: The restart-availability candidates turn on how Windows TCP actually behaves. The checkout cannot show this, and the sources disagree. Microsoft's SO_EXCLUSIVEADDRUSE documentation says a rebind is blocked while an accepted connection is still 'active' (ESTABLISHED, FIN_WAIT, FIN_WAIT_2 or LAST_ACK), and that a zero-window peer can keep it active. Trio's issue #928, however, reports empirical Windows testing in which this caveat did not occur. Settling it needs a native Windows run: restart a listening node immediately after inbound peers were connected, including a peer that has stopped reading. - verifier: Unresolved candidate: All three candidates share one root cause: replacing SO_REUSEADDR with SO_EXCLUSIVEADDRUSE on Windows might stop a restarted node from rebinding while connections accepted by the old listener are still closing. The code facts are verified. The base comment at src/net.cpp:3395-3396 explains SO_REUSEADDR as allowing a rebind during TIME_WAIT. At head, Windows sets only SO_EXCLUSIVEADDRUSE (3400). CloseSocketDisconnect does a plain m_sock.reset() (562-568), and StopNodes closes peer sockets before vhListenSocket.clear() (3766-3780). A failed IPv4 wildcard bind is fatal (3565-3566, 3577-3583). The OS-level premise, however, cannot be established. Microsoft's documentation counts only ESTABLISHED, FIN_WAIT, FIN_WAIT_2 and LAST_ACK as active, so design:1's TIME_WAIT premise conflicts with the documentation itself. The zero-window case (adversarial:1, adversarial_claude:1) is documented by Microsoft, but Trio's issue #928 reports that the caveat did not hold in empirical Windows testing. The RPC listener already merged in src/httpserver.cpp:725-734 has the same exposure, and Windows functional tests restart nodes that have peers. What is still needed is a native Windows run that restarts immediately after inbound peers, including one that has stopped reading with data queued. Review tier: sensitive [Full review report](<https://review.fish.foo/traces/36305-db31a3ad1f75538d07707d373adbfbe90ba8e328-1518-7.html>)
hodlinator (Migrated from github.com) reviewed 2026-10-23 11:24:33 +00:00
hodlinator (Migrated from github.com) left a comment

Concept ACK c6971fd610

Concept ACK c6971fd61036cf738606afa62826af47e4315730
Lines 3402-3403
hodlinator (Migrated from github.com) commented 2026-10-23 11:19:08 +00:00

Not sure why setting the option would fail, but I think we should treat it as a fatal error, same as in httpserver.cpp.

        strError = Untranslated(strprintf("Error setting SO_EXCLUSIVEADDRUSE on socket: %s", NetworkErrorString(WSAGetLastError())));
        LogError("%s\n", strError.original);
        return false;

https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse state:

All server applications must set SO_EXCLUSIVEADDRUSE for a strong level of socket security. Not only does it prevent malicious software from hijacking the port, but it also indicates whether or not another application is bound to the requested port. For example, a call to bind on the wildcard address by a process with the SO_EXCLUSIVEADDRUSE socket option set will fail if another process is currently bound to the same port on a specific interface.

Lastly, even though socket security has been improved in Windows Server 2003, an application should always set the SO_EXCLUSIVEADDRUSE socket option to ensure that it binds to all the specific interfaces the process has requested. The socket security in Windows Server 2003 adds an increased level of security for legacy applications, but application developers must still design their products with all aspects of security in mind.

Not sure why setting the option would fail, but I think we should treat it as a fatal error, same as in httpserver.cpp. ```suggestion strError = Untranslated(strprintf("Error setting SO_EXCLUSIVEADDRUSE on socket: %s", NetworkErrorString(WSAGetLastError()))); LogError("%s\n", strError.original); return false; ``` https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse state: > All server applications must set SO_EXCLUSIVEADDRUSE for a strong level of socket security. Not only does it prevent malicious software from hijacking the port, but it also indicates whether or not another application is bound to the requested port. For example, a call to [bind](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind) on the wildcard address by a process with the SO_EXCLUSIVEADDRUSE socket option set will fail if another process is currently bound to the same port on a specific interface. > > Lastly, even though socket security has been improved in Windows Server 2003, an application should always set the SO_EXCLUSIVEADDRUSE socket option to ensure that it binds to all the specific interfaces the process has requested. The socket security in Windows Server 2003 adds an increased level of security for legacy applications, but application developers must still design their products with all aspects of security in mind.
hodlinator (Migrated from github.com) commented 2026-10-23 11:22:09 +00:00

Could also add a test for when the socket is bound before bitcoind:

--- a/test/functional/feature_port.py
+++ b/test/functional/feature_port.py
@@ -11,6 +11,7 @@ import socket
 from test_framework.test_framework import (
     BitcoinTestFramework,
 )
+from test_framework.test_node import ErrorMatch
 from test_framework.util import (
     p2p_port,
     assert_raises,
@@ -41,6 +42,17 @@ class PortTest(BitcoinTestFramework):
             # listener requested for exclusive address use.
             assert_raises(OSError, lambda: competing_listener.bind(("0.0.0.0", port1)))

+        self.log.info("Verifying that we fail during startup if another process is bound to the port")
+        self.stop_node(0)
+        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as competing_listener:
+            competing_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+            self.log.info(f"Binding to {port1}, not {port2}")
+            competing_listener.bind(("0.0.0.0", port1))
+            node.assert_start_raises_init_error(
+                extra_args=["-listen", f"-port={port1}"],
+                expected_msg=fr"^Error\: Unable to bind to 0\.0\.0\.0\:{port1} on this computer\..*",
+                match=ErrorMatch.PARTIAL_REGEX)
+

(Edit: Realized the setsockopt() on the python side is probably not necessary).

Could also add a test for when the socket is bound before bitcoind: ```diff --- a/test/functional/feature_port.py +++ b/test/functional/feature_port.py @@ -11,6 +11,7 @@ import socket from test_framework.test_framework import ( BitcoinTestFramework, ) +from test_framework.test_node import ErrorMatch from test_framework.util import ( p2p_port, assert_raises, @@ -41,6 +42,17 @@ class PortTest(BitcoinTestFramework): # listener requested for exclusive address use. assert_raises(OSError, lambda: competing_listener.bind(("0.0.0.0", port1))) + self.log.info("Verifying that we fail during startup if another process is bound to the port") + self.stop_node(0) + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as competing_listener: + competing_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.log.info(f"Binding to {port1}, not {port2}") + competing_listener.bind(("0.0.0.0", port1)) + node.assert_start_raises_init_error( + extra_args=["-listen", f"-port={port1}"], + expected_msg=fr"^Error\: Unable to bind to 0\.0\.0\.0\:{port1} on this computer\..*", + match=ErrorMatch.PARTIAL_REGEX) + ``` (Edit: Realized the `setsockopt()` on the python side is probably not necessary).
winterrdog (Migrated from github.com) reviewed 2026-10-25 13:56:00 +00:00
Lines 3402-3403
winterrdog (Migrated from github.com) commented 2026-10-25 13:56:00 +00:00

correct, this is much better. fixed and thanks a lot!

correct, this is much better. fixed and thanks a lot!
winterrdog (Migrated from github.com) reviewed 2026-10-25 13:56:24 +00:00
winterrdog (Migrated from github.com) commented 2026-10-25 13:56:24 +00:00

i agreee, added the test

(Edit: Realized the setsockopt() on the python side is probably not necessary).

yes, we can leave out the Python-side setsockopt() because on Linux (and likely other POSIX systems), setting SO_REUSEADDR on both sockets would let bitcoind bind 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 (where bitcoind binds first) already catches that regression

i agreee, added the test > (Edit: Realized the `setsockopt()` on the python side is probably not necessary). yes, we can leave out the Python-side `setsockopt()` because on Linux (and likely other POSIX systems), setting `SO_REUSEADDR` on both sockets would let `bitcoind` bind 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 (where `bitcoind` binds first) already catches that regression
hodlinator (Migrated from github.com) approved these changes 2026-10-26 10:26:24 +00:00
hodlinator (Migrated from github.com) left a comment

ACK 36505a3992

Ran functional test on Windows. Thanks for taking my suggestions!

ACK 36505a3992f509510250b9701b617a35594dbff3 Ran functional test on Windows. Thanks for taking my suggestions!
hebasto (Migrated from github.com) reviewed 2026-10-26 12:56:50 +00:00
hebasto (Migrated from github.com) left a comment

Concept ACK.

Concept ACK.
hebasto (Migrated from github.com) reviewed 2026-10-27 13:31:02 +00:00
hebasto (Migrated from github.com) left a comment

I think the commits should be swapped. Otherwise, the first commit fails the test on Windows.

I think the commits should be swapped. Otherwise, the first commit fails the test on Windows.
winterrdog commented 2026-10-27 15:42:52 +00:00 (Migrated from github.com)

the commits should be swapped

done

> the commits should be swapped done
hodlinator (Migrated from github.com) approved these changes 2026-10-27 18:54:34 +00:00
hodlinator (Migrated from github.com) left a comment

re-ACK db31a3ad1f

I think the commits should be swapped. Otherwise, the first commit fails the test on Windows.

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

re-ACK db31a3ad1f75538d07707d373adbfbe90ba8e328 > I think the commits should be swapped. Otherwise, the first commit fails the test on Windows. Think 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.
hebasto (Migrated from github.com) approved these changes 2026-11-02 14:47:55 +00:00
hebasto (Migrated from github.com) left a comment

ACK db31a3ad1f.

ACK db31a3ad1f75538d07707d373adbfbe90ba8e328.
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!36305
No description provided.