{
 "number": 36445,
 "repo": "bitcoin/bitcoin",
 "url": "https://github.com/bitcoin/bitcoin/pull/36445",
 "title": "net: Skip cs_main when a peer has no orphan work",
 "author": "w0xlt",
 "author_association": "CONTRIBUTOR",
 "created_at": "2026-10-06T02:18:35Z",
 "updated_at": "2026-10-08T07:08:26Z",
 "description_hash": "00128651962b8df8",
 "age_days": 26,
 "draft": false,
 "labels": [
  "P2P"
 ],
 "milestone": null,
 "base": "master",
 "head_sha": "6549bb4bd6ae27c339c7a277bab9d48ed2fb730c",
 "head_ref": "net/orphan-check-without-cs-main",
 "head_repo": "w0xlt/bitcoin",
 "head_history": [],
 "additions": 38,
 "deletions": 0,
 "changed_files": 2,
 "commit_count": 1,
 "size_bucket": "S",
 "mergeable_state": "clean",
 "bot": {
  "drahtbot": {
   "present": true,
   "reviews": {},
   "conflicts": []
  }
 },
 "acks_parsed": {},
 "acks_tally": {
  "ack": 0,
  "stale_ack": 0,
  "concept_ack": 0,
  "approach_ack": 0,
  "nack": 0,
  "concept_nack": 0,
  "approach_nack": 0
 },
 "reviews": {
  "approved": 0,
  "changes_requested": 0,
  "distinct_reviewers": [
   "instagibbs"
  ]
 },
 "signals": {
  "needs_rebase": false,
  "ci_failed": false,
  "mergeable_state": "clean",
  "last_author_activity": "2026-10-07T05:41:40Z",
  "last_reviewer_activity": "2026-10-06T11:33:51Z",
  "last_reviewer": "instagibbs",
  "author_silent_days": 25,
  "waiting_on_author_days": 0,
  "days_since_update": 24
 },
 "refs": {
  "mentioned": [
   36244
  ],
  "depends_on": [],
  "fixes": [],
  "linked_issues": [],
  "references": [
   {
    "number": 36244,
    "type": "pull",
    "state": "open",
    "merged": false,
    "merged_at": null,
    "title": "validation, net: Process blocks asynchronously and reduce cs_main contention"
   }
  ],
  "conflicts": []
 },
 "stack": {
  "shares_commits_with": [],
  "based_on": [],
  "base_for": []
 },
 "review_paths": [],
 "body": "`ProcessMessages` takes `cs_main` for every peer on every handler pass, just to check for orphan transactions to reconsider, though most peers have none. This PR checks for orphan work under `m_tx_download_mutex` first and skips `cs_main` when there is none. Peers with orphan work take the same path as before, plus one uncontended lock.\n\nThis removes needless `cs_main` contention with validation, RPC and wallet threads. It's part of a broader effort to reduce `cs_main` contention in net processing (see #36244 for context), but stands on its own.\n\n| `ProcessMessages` handling a `ping` | master | this PR |\n|---|---|---|\n| no contention | 0.005 ms | 0.005 ms |\n| another thread holds `cs_main` for 50 ms | 50.1 ms | 0.02 ms |\n\nRelease builds, median of 3 runs. `SendMessages` in the same handler pass still takes `cs_main`.\n\nTested by `process_messages_without_orphan_work`, which fails without this change.",
 "commits": [
  {
   "sha": "6549bb4bd6ae27c339c7a277bab9d48ed2fb730c",
   "date": "2026-10-03T21:14:16Z",
   "message": "net: Skip cs_main when a peer has no orphan work\n\nProcessMessages reconsiders a peer's orphan transactions before taking\nits next message, and took cs_main for this even when the peer had\nnone. Check for orphan work under m_tx_download_mutex first, so that a\nmessage from a peer without orphan work does not wait for a busy\nchainstate.\n\nTest that ProcessMessages completes for such a peer while cs_main is\nheld."
  }
 ],
 "timeline": [
  {
   "t": "2026-10-06T11:33:51Z",
   "kind": "comment",
   "who": "instagibbs",
   "assoc": "MEMBER",
   "text": "I think these kind of refactors only make sense if/when https://github.com/bitcoin/bitcoin/pull/36244 is an accepted goal by the project\n\nI have not actually looked, just noting for this PR's sake.",
   "id": "c:6015364052",
   "url": "https://github.com/bitcoin/bitcoin/pull/36445#issuecomment-6015364052",
   "hash": "95774d0d7055360b",
   "edited": null
  },
  {
   "t": "2026-10-06T15:47:17Z",
   "kind": "comment",
   "who": "w0xlt",
   "assoc": "CONTRIBUTOR",
   "text": "This PR stands on its own, though: skipping `cs_main` when a peer has no orphan work helps on master today, with or without #36244.\n#36244 just benefits more from it.",
   "id": "c:6019981554",
   "url": "https://github.com/bitcoin/bitcoin/pull/36445#issuecomment-6019981554",
   "hash": "5f53946c43592376",
   "edited": "2026-10-06T16:04:07Z"
  },
  {
   "t": "2026-10-07T05:41:40Z",
   "kind": "comment",
   "who": "w0xlt",
   "assoc": "CONTRIBUTOR",
   "text": "PR description updated for greater clarity.",
   "id": "c:6031763631",
   "url": "https://github.com/bitcoin/bitcoin/pull/36445#issuecomment-6031763631",
   "hash": "f19489abcdda4453",
   "edited": null
  }
 ],
 "labels_log": [
  {
   "t": "2026-10-06T02:18:38Z",
   "action": "labeled",
   "label": "P2P",
   "who": "DrahtBot"
  }
 ],
 "state_log": [],
 "text_chars": 1781,
 "text_tokens_estimate": 445,
 "changed_paths": [
  "src/net_processing.cpp",
  "src/test/peerman_tests.cpp"
 ],
 "files": [
  {
   "path": "src/net_processing.cpp",
   "add": 2,
   "del": 0
  },
  {
   "path": "src/test/peerman_tests.cpp",
   "add": 36,
   "del": 0
  }
 ],
 "test_lines": 36,
 "git": {
  "head": "6549bb4bd6ae27c339c7a277bab9d48ed2fb730c",
  "head_matches_backup": true,
  "base": "66776840beb558f7e84451c2c55457f0e06242f0",
  "patch_id": "0a8677f44c566054",
  "commits": [
   {
    "sha": "6549bb4bd6",
    "subject": "net: Skip cs_main when a peer has no orphan work",
    "files": 2,
    "add": 38,
    "del": 0
   }
  ],
  "patch_truncated": false
 },
 "patch_id": "0a8677f44c566054",
 "input_hash": "1fab73a8dbd97e54",
 "extracted_at": "2026-11-02T01:09:49+00:00"
}