#36445 net: Skip cs_main when a peer has no orphan work

ReviewabilityReviewsAgreementSize
#36445 w0xlt net: Skip cs_main when a peer has no orphan workReady0Crickets2 + 36 tests
  • Prevent peer message processing from blocking on cs_main when a peer has no orphan work queued
  • Avoid unnecessary latency in handling P2P messages while validation, RPC, or wallet threads hold the lock
  • Ready to review
  • Small two-line change with an accompanying unit test
  • No review verdicts recorded
  • No substantive reviews yet
  • Suggested only pursuing this if refactoring in #36244 is an accepted goal (instagibbs)
  • Author replied that avoiding cs_main contention has standalone value on master today
  • 2 lines added or modified outside tests
  • 36 lines added or modified in tests
  • 0 lines removed in total
  • 2 files, 1 commits
P2P#21 of 71
P2 · speedup
  • P2 because it eliminates unconditional cs_main lock contention during routine peer message processing
  • Prevents validation and RPC activity from stalling message loops when no orphan work is pending
  • Microbenchmarks show ping handling latency dropping from 50 ms to 0.02 ms under lock contention
  • Ranking pass: Measured responsiveness win in the message loop; grouped with the other cs_main contention work.
Reviewability: ReadyStalePaused
Agreement: StrongPositivePositive w/ caveatsNeutralMildDisputedBlockedCrickets
Reviews: current code-review ACKs, then (+stale ACKs) and -NACKs; greener = more ACKs.
Size: lines added or modified outside tests, then in tests; greener = smaller.

Goal

Check whether a peer has orphan transactions waiting under m_tx_download_mutex before acquiring cs_main in ProcessOrphanTx. When no orphan work is queued, ProcessOrphanTx returns immediately without contending for cs_main. A unit test verifies ProcessMessages completes while cs_main is held elsewhere.

Problem: ProcessMessages routinely calls ProcessOrphanTx on each message-processing pass, acquiring cs_main unconditionally even when a peer has no orphan work. P2P message processing for that peer then blocks whenever validation, RPC, or wallet threads hold cs_main.

Evidence of importance:

  • Author microbenchmarks show ProcessMessages handling a ping taking 50.1 ms on master when cs_main is held for 50 ms by another thread, dropping to 0.02 ms with this change.
  • A unit test (process_messages_without_orphan_work) reproduces the block on master and passes with this change.

Scope: Small two-line change in net_processing.cpp paired with a multithreaded unit test in peerman_tests.cpp.

Risk of a mistake: low (p2p). A race condition or locking bug here could delay orphan transaction processing or cause deadlocks during P2P message handling.

Reviewability

Ready

Nothing known that would invalidate a review now.

Waiting on (from the thread read):

  • reviewer: review of the PR and response to author explanation 2026-10-06

Agreement

Crickets

no substantive comment either way

Thread notes: Author updated the PR description for clarity; no new claims or reviews were added.

Objections, suggestions, and questions:

ReviewerTypeHarm, request, or questionStatusMust beAuthor repliedQuote
instagibbs (member)suggestion, usefulnessOnly pursue this refactor if/when #36244 is an accepted goal for the projectanswered · author explained that skipping cs_main without orphan work is beneficial on master on its ownansweredyes2026-10-06: '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'
Status from: 2026-10-06: w0xlt: 'This PR stands on its own, though: skipping `cs_main` when a peer has no orphan work helps on master today, with or with'

Participants:

  • instagibbs (member, neutral, 1 statements 2026-10-06 to 2026-10-06): noted that such refactors may only make sense if #36244 is an accepted project goal

Files

36 lines under test/bench/ci.

  • src/test/peerman_tests.cpp +36/-0
  • src/net_processing.cpp +2/-0

Card

The short text the ranking pass compares across PRs.

Skips acquiring cs_main in ProcessOrphanTx when a peer has no orphan transactions queued, checking under m_tx_download_mutex first. Currently, ProcessMessages acquires cs_main unconditionally on every peer handler pass, blocking P2P message processing whenever validation, RPC, or wallet threads hold cs_main. In author benchmarks where cs_main was held for 50 ms, ping handling latency dropped from 50.1 ms to 0.02 ms. The PR is self-contained and related to broader lock-reduction work in #36244.

Data

dossier JSON · extract JSON · model ledger, generated 2026-10-08T01:20, confidence high, input hash 20d19bc73bc0f4ba
thread read through 2026-10-07 (3 statements), head 6549bb4bd6
labels: P2P

Categories

P2P

P2 · speedup (ranking pass: Measured responsiveness win in the message loop; grouped with the other cs_main contention work.)

Eliminating unconditional `cs_main` acquisition in `ProcessOrphanTx` prevents peer message processing from blocking whenever validation, RPC, or wallet threads hold the lock. Microbenchmarks showed ping handling latency dropping from 50.1 ms to 0.02 ms during concurrent lock contention.

Membership: Touches `src/net_processing.cpp` to optimize peer message handling and carries the P2P label.

Factors: security/stability 1, bug 0, performance 2, user value 0, leverage 0