{
 "number": 36305,
 "repo": "bitcoin/bitcoin",
 "url": "https://github.com/bitcoin/bitcoin/pull/36305",
 "title": "net: Use exclusive P2P listen sockets on Windows",
 "author": "winterrdog",
 "author_association": "CONTRIBUTOR",
 "created_at": "2026-09-20T22:11:54Z",
 "updated_at": "2026-10-27T18:54:38Z",
 "description_hash": "2ddb59ff671d1ff4",
 "age_days": 42,
 "draft": false,
 "labels": [
  "Windows",
  "P2P"
 ],
 "milestone": null,
 "base": "master",
 "head_sha": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
 "head_ref": "fix/p2p-exclusive-bind-win32",
 "head_repo": "winterrdog/bitcoin",
 "head_history": [
  {
   "t": "2026-10-25T13:55:11Z",
   "sha": "36505a3992f509510250b9701b617a35594dbff3"
  },
  {
   "t": "2026-10-27T15:42:41Z",
   "sha": "db31a3ad1f75538d07707d373adbfbe90ba8e328"
  }
 ],
 "additions": 31,
 "deletions": 0,
 "changed_files": 2,
 "commit_count": 2,
 "size_bucket": "S",
 "mergeable_state": "clean",
 "bot": {
  "drahtbot": {
   "present": true,
   "reviews": {
    "ack": [
     {
      "login": "jpk68",
      "url": "https://github.com/bitcoin/bitcoin/pull/36305#issuecomment-6281543300"
     },
     {
      "login": "hodlinator",
      "url": "https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5672657235"
     }
    ],
    "concept_ack": [
     {
      "login": "hebasto",
      "url": "https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5652305051"
     }
    ]
   },
   "conflicts": []
  }
 },
 "acks_parsed": {
  "jpk68": {
   "kind": "ack",
   "hash": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "t": "2026-10-20T01:58:32Z",
   "stale": false
  },
  "hodlinator": {
   "kind": "ack",
   "hash": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "t": "2026-10-27T18:54:34Z",
   "stale": false
  },
  "hebasto": {
   "kind": "concept_ack",
   "hash": null,
   "t": "2026-10-26T12:56:50Z",
   "stale": false
  }
 },
 "acks_tally": {
  "ack": 2,
  "stale_ack": 0,
  "concept_ack": 1,
  "approach_ack": 0,
  "nack": 0,
  "concept_nack": 0,
  "approach_nack": 0
 },
 "reviews": {
  "approved": 2,
  "changes_requested": 0,
  "distinct_reviewers": [
   "hebasto",
   "hodlinator",
   "jpk68"
  ]
 },
 "signals": {
  "needs_rebase": false,
  "ci_failed": false,
  "mergeable_state": "clean",
  "last_author_activity": "2026-10-27T15:42:52Z",
  "last_reviewer_activity": "2026-10-27T18:54:34Z",
  "last_reviewer": "hodlinator",
  "author_silent_days": 5,
  "waiting_on_author_days": 5,
  "days_since_update": 5
 },
 "refs": {
  "mentioned": [
   36169
  ],
  "depends_on": [],
  "fixes": [],
  "linked_issues": [],
  "references": [
   {
    "number": 36169,
    "type": "pull",
    "state": "closed",
    "merged": true,
    "merged_at": "2026-09-06",
    "title": "http: Use SO_EXCLUSIVEADDRUSE on Windows"
   }
  ],
  "conflicts": []
 },
 "stack": {
  "shares_commits_with": [],
  "based_on": [],
  "base_for": []
 },
 "review_paths": [
  "src/net.cpp",
  "test/functional/feature_port.py"
 ],
 "body": "on Windows, `SO_REUSEADDR` does not carry over the same semantics as on\nPOSIX systems. a second socket can set `SO_REUSEADDR` and bind to a\nport that is _already in use_ by another process running under\nthe _same_ user account. this means another local process could bind to\nthe P2P listen port and receive incoming connections intended for\n`bitcoind`.\n\nthis matters because the P2P listener is expected to have exclusive\nownership of its address and port. allowing another process to bind to\nit could let that process _intercept_ incoming P2P connections. this is\nin a similar spirit to #36169, which made the RPC listener exclusive on\nWindows.\n\nto fix this, we use `SO_EXCLUSIVEADDRUSE` for the P2P listen socket on\nWindows instead of `SO_REUSEADDR`, while retaining `SO_REUSEADDR` on\nother platforms. the added test verifies the expected behaviour by\nattempting to bind a second socket to the same port. on Windows, this\nfails with `WSAEACCES` (`10013`), which Python exposes as\n`PermissionError` (an `OSError` subclass).\n\nreferences:\n\n- https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse\n- https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ/14388707#14388707",
 "commits": [
  {
   "sha": "0dbd271eb4a3263b8b81ccf22876429076bae574",
   "date": "2026-10-27T15:41:57Z",
   "message": "net: use exclusive P2P listen sockets on Windows\n\nOn Windows, `SO_REUSEADDR` does not give exclusive use of an address and\nport, so another process under the same user account can bind the same\nport and intercept incoming P2P connections.\n\nOn Windows, use `SO_EXCLUSIVEADDRUSE` instead. If the option cannot be\nset, fail the bind rather than listen on a port that could be hijacked."
  },
  {
   "sha": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "date": "2026-10-27T15:41:57Z",
   "message": "test: check P2P listen port exclusivity\n\nCheck both orderings of a port conflict: another socket cannot bind the\nP2P port while `bitcoind` is listening on it, and `bitcoind` refuses to\nstart if another socket already holds the port.\n\nCo-authored-by: hodlinator <172445034+hodlinator@users.noreply.github.com>"
  }
 ],
 "timeline": [
  {
   "t": "2026-09-20T22:12:42Z",
   "kind": "comment",
   "who": "winterrdog",
   "assoc": "CONTRIBUTOR",
   "text": "indeed, this difference in `SO_REUSEADDR` behaviour across operating systems is\nsomewhat surprising, and has sparked similar discussions in Tornado,\nCPython, and Trio.\n\n- https://github.com/python-trio/trio/issues/39\n- https://github.com/tornadoweb/tornado/issues/550\n- https://github.com/python/cpython/issues/46802",
   "id": "c:5753047298",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#issuecomment-5753047298",
   "hash": "26207145d27d4530",
   "edited": "2026-09-25T07:19:09Z"
  },
  {
   "t": "2026-10-20T01:58:32Z",
   "kind": "comment",
   "who": "jpk68",
   "assoc": "CONTRIBUTOR",
   "text": "ACK db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "id": "c:6281543300",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#issuecomment-6281543300",
   "hash": "8f84354596867cf1",
   "edited": "2026-10-27T16:42:56Z"
  },
  {
   "t": "2026-10-23T11:19:08Z",
   "kind": "review_comment",
   "who": "hodlinator",
   "assoc": "MEMBER",
   "path": "src/net.cpp",
   "commit": "c6971fd61036cf738606afa62826af47e4315730",
   "in_reply_to": null,
   "text": "Not sure why setting the option would fail, but I think we should treat it as a fatal error, same as in httpserver.cpp.\n```suggestion\n        strError = Untranslated(strprintf(\"Error setting SO_EXCLUSIVEADDRUSE on socket: %s\", NetworkErrorString(WSAGetLastError())));\n        LogError(\"%s\\n\", strError.original);\n        return false;\n```\n\nhttps://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse state:\n[quoted text omitted]",
   "id": "rc:4358954387",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#discussion_r4358954387",
   "hash": "698117b17effb83d",
   "edited": "2026-10-23T11:24:33Z"
  },
  {
   "t": "2026-10-23T11:22:09Z",
   "kind": "review_comment",
   "who": "hodlinator",
   "assoc": "MEMBER",
   "path": "test/functional/feature_port.py",
   "commit": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "in_reply_to": null,
   "text": "Could also add a test for when the socket is bound before bitcoind:\n```diff\n--- a/test/functional/feature_port.py\n+++ b/test/functional/feature_port.py\n@@ -11,6 +11,7 @@ import socket\n from test_framework.test_framework import (\n     BitcoinTestFramework,\n )\n+from test_framework.test_node import ErrorMatch\n from test_framework.util import (\n     p2p_port,\n     assert_raises,\n@@ -41,6 +42,17 @@ class PortTest(BitcoinTestFramework):\n             # listener requested for exclusive address use.\n             assert_raises(OSError, lambda: competing_listener.bind((\"0.0.0.0\", port1)))\n\n+        self.log.info(\"Verifying that we fail during startup if another process is bound to the port\")\n+        self.stop_node(0)\n+        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as competing_listener:\n+            competing_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n+            self.log.info(f\"Binding to {port1}, not {port2}\")\n+            competing_listener.bind((\"0.0.0.0\", port1))\n+            node.assert_start_raises_init_error(\n+                extra_args=[\"-listen\", f\"-port={port1}\"],\n+                expected_msg=fr\"^Error\\: Unable to bind to 0\\.0\\.0\\.0\\:{port1} on this computer\\..*\",\n+                match=ErrorMatch.PARTIAL_REGEX)\n+\n```\n(Edit: Realized the `setsockopt()` on the python side is probably not necessary).",
   "id": "rc:4358977350",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#discussion_r4358977350",
   "hash": "26a12c65614c3fca",
   "edited": "2026-10-23T11:39:55Z"
  },
  {
   "t": "2026-10-23T11:24:33Z",
   "kind": "review",
   "who": "hodlinator",
   "assoc": "MEMBER",
   "state": "COMMENTED",
   "commit": "c6971fd61036cf738606afa62826af47e4315730",
   "text": "Concept ACK c6971fd61036cf738606afa62826af47e4315730",
   "id": "r:5627501696",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5627501696",
   "hash": "3cdf366befe1d3ac",
   "edited": null
  },
  {
   "t": "2026-10-25T13:55:11Z",
   "kind": "force_push",
   "who": "winterrdog",
   "commit": "36505a3992f509510250b9701b617a35594dbff3",
   "id": "push:36505a3992f509510250b9701b617a35594dbff3"
  },
  {
   "t": "2026-10-25T13:56:00Z",
   "kind": "review_comment",
   "who": "winterrdog",
   "assoc": "CONTRIBUTOR",
   "path": "src/net.cpp",
   "commit": "c6971fd61036cf738606afa62826af47e4315730",
   "in_reply_to": 4358954387,
   "text": "correct, this is much better. fixed and thanks a lot!",
   "id": "rc:4371079797",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#discussion_r4371079797",
   "hash": "717471f848b64378",
   "edited": "2026-10-25T13:56:01Z"
  },
  {
   "t": "2026-10-25T13:56:24Z",
   "kind": "review_comment",
   "who": "winterrdog",
   "assoc": "CONTRIBUTOR",
   "path": "test/functional/feature_port.py",
   "commit": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "in_reply_to": 4358977350,
   "text": "i agreee, added the test\n\n[quoted text omitted]\nyes, 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",
   "id": "rc:4371080995",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#discussion_r4371080995",
   "hash": "aeb7d13ec02d71eb",
   "edited": "2026-10-25T14:09:57Z"
  },
  {
   "t": "2026-10-26T10:26:24Z",
   "kind": "review",
   "who": "hodlinator",
   "assoc": "MEMBER",
   "state": "APPROVED",
   "commit": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "text": "ACK 36505a3992f509510250b9701b617a35594dbff3\n\nRan functional test on Windows. Thanks for taking my suggestions!",
   "id": "r:5650576646",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5650576646",
   "hash": "1e1ff776a4a89712",
   "edited": "2026-10-27T15:42:41Z"
  },
  {
   "t": "2026-10-26T12:56:50Z",
   "kind": "review",
   "who": "hebasto",
   "assoc": "MEMBER",
   "state": "COMMENTED",
   "commit": "36505a3992f509510250b9701b617a35594dbff3",
   "text": "Concept ACK.",
   "id": "r:5652305051",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5652305051",
   "hash": "39bcd188b11b7a83",
   "edited": null
  },
  {
   "t": "2026-10-27T13:31:02Z",
   "kind": "review",
   "who": "hebasto",
   "assoc": "MEMBER",
   "state": "COMMENTED",
   "commit": "36505a3992f509510250b9701b617a35594dbff3",
   "text": "I think the commits should be swapped. Otherwise, the first commit fails the test on Windows.",
   "id": "r:5667835205",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5667835205",
   "hash": "41485d97df5022da",
   "edited": null
  },
  {
   "t": "2026-10-27T15:42:41Z",
   "kind": "force_push",
   "who": "winterrdog",
   "commit": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "id": "push:db31a3ad1f75538d07707d373adbfbe90ba8e328"
  },
  {
   "t": "2026-10-27T15:42:52Z",
   "kind": "comment",
   "who": "winterrdog",
   "assoc": "CONTRIBUTOR",
   "text": "[quoted text omitted]\n\ndone",
   "id": "c:6446436450",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#issuecomment-6446436450",
   "hash": "8a1da63f179c110b",
   "edited": null
  },
  {
   "t": "2026-10-27T18:54:34Z",
   "kind": "review",
   "who": "hodlinator",
   "assoc": "MEMBER",
   "state": "APPROVED",
   "commit": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
   "text": "re-ACK db31a3ad1f75538d07707d373adbfbe90ba8e328\n\n[quoted text omitted]\nThink 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.",
   "id": "r:5672657235",
   "url": "https://github.com/bitcoin/bitcoin/pull/36305#pullrequestreview-5672657235",
   "hash": "45071201e0f7a5e7",
   "edited": null
  }
 ],
 "labels_log": [
  {
   "t": "2026-09-20T22:11:57Z",
   "action": "labeled",
   "label": "P2P",
   "who": "DrahtBot"
  },
  {
   "t": "2026-09-21T06:05:27Z",
   "action": "labeled",
   "label": "Windows",
   "who": "maflcko"
  }
 ],
 "state_log": [],
 "text_chars": 5204,
 "text_tokens_estimate": 1301,
 "changed_paths": [
  "src/net.cpp",
  "test/functional/feature_port.py"
 ],
 "files": [
  {
   "path": "src/net.cpp",
   "add": 12,
   "del": 0
  },
  {
   "path": "test/functional/feature_port.py",
   "add": 19,
   "del": 0
  }
 ],
 "test_lines": 19,
 "git": {
  "head": "db31a3ad1f75538d07707d373adbfbe90ba8e328",
  "head_matches_backup": true,
  "base": "d48e76e689bb4d680d9d8a70b67f994be2f0cdd1",
  "patch_id": "17ee28726e3b24dc",
  "commits": [
   {
    "sha": "0dbd271eb4",
    "subject": "net: use exclusive P2P listen sockets on Windows",
    "files": 1,
    "add": 12,
    "del": 0
   },
   {
    "sha": "db31a3ad1f",
    "subject": "test: check P2P listen port exclusivity",
    "files": 1,
    "add": 19,
    "del": 0
   }
  ],
  "patch_truncated": false
 },
 "patch_id": "17ee28726e3b24dc",
 "input_hash": "5311fd8bfe48b0b8",
 "extracted_at": "2026-11-02T01:09:49+00:00"
}