Skip to content

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

Open
w0xlt wants to merge 1 commit into
bitcoin:masterfrom
w0xlt:net/orphan-check-without-cs-main
Open

w0xlt wants to merge 1 commit into
bitcoin:masterfrom
w0xlt:net/orphan-check-without-cs-main

Conversation

@w0xlt

@w0xlt w0xlt commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

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.

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

ProcessMessages handling a ping master this PR
no contention 0.005 ms 0.005 ms
another thread holds cs_main for 50 ms 50.1 ms 0.02 ms

Release builds, median of 3 runs. SendMessages in the same handler pass still takes cs_main.

Tested by process_messages_without_orphan_work, which fails without this change.

ProcessMessages reconsiders a peer's orphan transactions before taking
its next message, and took cs_main for this even when the peer had
none. Check for orphan work under m_tx_download_mutex first, so that a
message from a peer without orphan work does not wait for a busy
chainstate.

Test that ProcessMessages completes for such a peer while cs_main is
held.
@DrahtBot DrahtBot added the P2P label Oct 6, 2026
@DrahtBot

DrahtBot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

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.
A summary of reviews will appear here.

Conflicts

No conflicts as of last run.

@instagibbs

Copy link
Copy Markdown
Member

I think these kind of refactors only make sense if/when #36244 is an accepted goal by the project

I have not actually looked, just noting for this PR's sake.

@w0xlt

w0xlt commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

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.
#36244 just benefits more from it.

@w0xlt

w0xlt commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

PR description updated for greater clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants