forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
perf: make m_peer_mutex a SharedMutex #6468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PastaPastaPasta
merged 1 commit into
dashpay:develop
from
PastaPastaPasta:perf-shared-mutex-m_peer_map
Oct 26, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
SharedMutex swap looks good; confirm macro support/fairness.
Change to
SharedMutexis appropriate for read-heavy access. Please confirm:READ_LOCK(...)is defined by our sync wrappers and mapped to a shared-lock type.🏁 Script executed:
Length of output: 205545
I can confirm the macro support is in place. Now I need to verify the actual code at the flagged lines to assess the lifetime hazard mentioned in the scratchpad.
I need to verify the actual code at the flagged lines to assess the lifetime hazard mentioned in the scratchpad.
🏁 Script executed:
Length of output: 1797
🏁 Script executed:
Length of output: 2935
Fix the lifetime hazard in RelayAddress where raw peer pointers are used after releasing the lock.
The
READ_LOCK(...)macro is properly supported (defined at src/sync.h:343 and mapped toReadLock<decltype(cs)>on std::shared_mutex). However, RelayAddress stores rawPeer*pointers in thebestarray at line 2522, then uses them after the lock scope ends at line 2532. If a peer is removed fromm_peer_mapbetween lock release and thePushAddresscall, this becomes a use-after-free.Lines 2515–2528 (RelayAddress):
Capture
PeerRef(shared_ptr, line 439) instead of raw pointers to extend peer lifetime beyond the lock. Replacepeer.get()with the shared_ptr directly, or collect intostd::vector<PeerRef>likeAskPeersForTransactiondoes (line 2310).The other READ_LOCK sites (
_RelayTransaction,RelayRecoveredSig) are safe because all peer access occurs within the lock scope.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please look at this again;
For the first case, it appears to me the READ_LOCK is held through the destruction of the
bestarray. I don't see a UAF.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, something went wrong! Please try again later. 🐰 💔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please look at this again;
For the first case, it appears to me the READ_LOCK is held through the destruction of the best array. I don't see a UAF.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PastaPastaPasta You’re right — in RelayAddress the READ_LOCK(m_peer_mutex) is acquired in the function scope and remains held through both loops, including the PushAddress calls. With the shared lock held, m_peer_map cannot be modified by writers, so the Peer* captured into best stays valid during its use. No UAF there. My earlier concern was unfounded — thanks for the nudge.
Optional: if you ever want to reduce the time m_peer_mutex is held, you could collect PeerRef entries under the read lock, release it, then call PushAddress on those refs. Not necessary for correctness here, just a potential micro-optimization.