Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
dergoegge
left a comment
There was a problem hiding this comment.
Code review ACK 765f659
The changes themselves look fine/safe to me but I think it might be useful to provide more motivation for this change.
The m_num_preferred_download_peers change also seems correct to me, but I found the previous version with a shared lock (i.e. cs_main) a little easier to reason about wrt "at anytime m_num_preferred_download_peers reflects the number of CNodeStates with fPreferredDownload equals true".
I started doing this to see how complicated the change would be to remove CNodeState and the cs_main locking it requires, I have more branches that move some of the other fields. I think tracking multiple states (CNodeState & Peer) is more confusing to reason about than just having one state and I think? it's been a goal to reduce the scope of cs_main (i.e. #27502). I have a private branch that removes some other fields, though there are some gotchas with locking.
Same. I think in this specific case, it's possible to replace the atomic with a smaller lock than cs_main. However, I don't think this is true of some other fields due to potential deadlock warnings that I haven't figured out how to fix. I think a smaller lock would be clearer than having two atomics here, open to changing it. |
mzumsande
left a comment
There was a problem hiding this comment.
Concept ACK
Same. I think in this specific case, it's possible to replace the atomic with a smaller lock than cs_main. However, I don't think this is true of some other fields due to potential deadlock warnings that I haven't figured out how to fix. I think a smaller lock would be clearer than having two atomics here, open to changing it.
to be fair, we do have the same pattern with m_wtxid_relay / m_wtxid_relay_peers which are also two atomics, so no strong opinion.
765f659 to
3861ab4
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
3861ab4 to
9686707
Compare
|
Seems like there is agreement to do this. Maybe one more rebase to get it over the line? Also cc @instagibbs |
|
ACK once rebased |
Both m_requested_hb_cmpctblocks & m_provides_cmpctblocks were protected by cs_main which is unnecessary. Instead convert them to std::atomic<bool> as Mutex is not needed. This way we don't need to worry about potential lock inversion in the future or thread safety annotations. Also, add a negative annotation to NewPoWValidBlock for m_peer_mutex since it now acquires it.
Also make the m_num_preferred_download_peers counter atomic and add an assert in FinalizeNode. The asserts for m_num_preferred_download_peers in FinalizeNode are still safe even without cs_main locking because of the implicit guarantee that ProcessMessage may not run for a peer at the same time as FinalizeNode. This has the nice benefit of being able to remove cs_main usage in some places.
-BEGIN VERIFY SCRIPT- sed -i "s/fPreferredDownload/m_preferred_download/g" $( git grep -l fPreferredDownload ) -END VERIFY SCRIPT-
9686707 to
4b02684
Compare

The first commit moves
m_requested_hb_cmpctblocksandm_provides_cmpctblocksfrom CNodeState to Peer and also makes the two fields atomic.The second commit moves
fPreferredDownloadto Peer and makes it atomic. Sincem_num_preferred_download_peersis a counter based on this field, it can also be atomic. This also lets us dropcs_mainusage in a few places. I found this commit harder to reason about, specifically whether the asserts in FinalizeNode were still safe form_num_preferred_download_peersif another thread could modify the value before/after them_num_preferred_download_peers -= peer->fPreferredDownloadline and trigger the assert. Afaict, these asserts are still safe because ProcessMessage cannot run for the same peer as the peer being cleaned up in FinalizeNode. I am also happy to drop this second commit or explain my reasoning in more detail.For reference:
m_requested_hb_cmpctblockscan be accessed from:NewPoWValidBlock(read) (b-msghand, http, ipc? threads)ProcessMessage(write) (b-msghand)SendMessages(read) (b-msghand)m_provides_cmpctblockscan be accessed from:MaybeSetPeerAsAnnouncingHeaderAndIDs(read) (b-msghand, http, ipc?)HeadersDirectFetchBlocks(read) (b-msghand)ProcessMessage(write) (b-msghand)fPreferredDownloadcan be accessed from:ProcessMessage(read/write) (b-msghand)SendMessages(read) (b-msghand)FinalizeNode(read) (bitcoind, b-net)m_num_preferred_download_peerscan be accessed from:ProcessMessage(write) (b-msghand)SendMessages(read) (b-msghand)FinalizeNode(read/write) (bitcoind, b-net)