Skip to content

Conversation

dergoegge
Copy link
Member

nUnconnectingHeaders, m_headers_sync_timeout, fPreferHeaders and m_recently_announced_headers are currently all CNodeState members even though they are only ever accessed from the message processing thread (therefore sufficiently guarded exclusively by g_msgproc_mutex). CNodeState exists purely to hold validation-specific state guarded by cs_main that is accessed by multiple threads.

This PR adds thread-safety annotations for the above mentioned CNodeState members and moves them to Peer.

@ajtowns
Copy link
Contributor

ajtowns commented Sep 20, 2022

Concept ACK

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 21, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK glozow, hebasto
Concept ACK ajtowns
Approach ACK pablomartin4btc
Stale ACK hernanmarino

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26621 (refactor: Continue moving application data from CNode to Peer by dergoegge)
  • #26283 (p2p: Fill reconciliation sets and request reconciliation (Erlay) by naumenkogs)
  • #25908 (p2p: remove adjusted time by fanquake)

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.

@hebasto
Copy link
Member

hebasto commented Sep 21, 2022

Concept ACK.

@dergoegge dergoegge force-pushed the 2022-09-move-cnodestate-msgproc-members branch from 8c22a6e to ca898b4 Compare October 24, 2022 14:27
@dergoegge dergoegge force-pushed the 2022-09-move-cnodestate-msgproc-members branch from ca898b4 to cafaa98 Compare October 28, 2022 09:35
@dergoegge
Copy link
Member Author

Rebased to avoid the p2p_sendtxrcncl.py failure.

Copy link
Contributor

@hernanmarino hernanmarino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK

Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK. I agree with the thread-safety annotation and moving CNodeState members into Peer which concurs with @jnewbery's original refactoring intentions on P2P -> #19398.

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

light code review ACK de97ab2

-BEGIN VERIFY SCRIPT-
ren() { sed -i "s:\<$1\>:$2:g" $(git grep -l "\<$1\>" ./src ./test); }

ren nUnconnectingHeaders     m_num_unconnecting_headers_msgs
ren fPreferHeaders           m_prefers_headers
ren MAX_UNCONNECTING_HEADERS MAX_NUM_UNCONNECTING_HEADERS_MSGS

-END VERIFY SCRIPT-
@dergoegge dergoegge force-pushed the 2022-09-move-cnodestate-msgproc-members branch from 37bb332 to 3a060ae Compare March 30, 2023 12:58
@dergoegge
Copy link
Member Author

Rebased

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code review ACK 3a060ae, as in I am convinced these members shouldn't be guarded by cs_main and belong in Peer/TxRelay. clang checked the annotations for me.

@DrahtBot DrahtBot requested a review from hernanmarino March 30, 2023 13:05
@glozow glozow requested review from hebasto and removed request for hernanmarino March 30, 2023 13:05
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 3a060ae

@DrahtBot DrahtBot requested a review from hernanmarino March 30, 2023 14:05
@hebasto
Copy link
Member

hebasto commented Mar 30, 2023

FWIW, I have no issues with TSan-instrumented builds locally using both clang-14 and clang-15. Also the "TSan, depends, gui" CI task passed for me locally.

@DrahtBot DrahtBot requested review from hernanmarino and removed request for hernanmarino March 30, 2023 15:26
@glozow glozow merged commit d4833e9 into bitcoin:master Mar 30, 2023
@glozow glozow removed their assignment Mar 30, 2023
@@ -2248,7 +2258,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
}
}

CTransactionRef PeerManagerImpl::FindTxForGetData(const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in a const TxRelay& here instead of a const Peer&?

@@ -2263,7 +2273,7 @@ CTransactionRef PeerManagerImpl::FindTxForGetData(const CNode& peer, const GenTx
{
LOCK(cs_main);
// Otherwise, the transaction must have been announced recently.
if (State(peer.GetId())->m_recently_announced_invs.contains(gtxid.GetHash())) {
if (Assume(peer.GetTxRelay())->m_recently_announced_invs.contains(gtxid.GetHash())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the LOCK(cs_main) now (and the LOCKS_EXCLUDED(cs_main) annotation from the function declaration)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems like it. Will do this in a follow up (including your other suggestion), thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this doesn't work because mapRelay is guraded by cs_main😭😭😭

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But... mapRelay is actually only accessed from the msg proc thread, so we can simply mark it as guarded by g_msgproc_mutex and still do this!

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 1, 2023
fanquake added a commit to bitcoin-core/gui that referenced this pull request Apr 2, 2023
3fa4c54 [net processing] Pass TxRelay to FindTxForGetData instead of Peer (dergoegge)
c85ee76 [net processin] Don't take cs_main in FindTxForGetData (dergoegge)

Pull request description:

  Addresses left over feedback from #26140.

  * bitcoin/bitcoin#26140 (comment)
  * bitcoin/bitcoin#26140 (comment)

  `mapRelay` is only accessed from the message processing thread and does not need to be kept in sync with anything validation specific, it is therfore perfectly fine to have it guarded by `g_msgproc_mutex`.

ACKs for top commit:
  jnewbery:
    utACK 3fa4c54
  hebasto:
    ACK 3fa4c54, I have reviewed the code and it looks OK, I agree it can be merged.

Tree-SHA512: 3ef84bfe4abfa8d991a7e65d9184221294d80e0df0bbb47f0270ab6ca1593266c98abf83c610f9f86b4d16c7a4b62bcf83f8856c68d3c2e10894bff6ed3e88cd
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 2, 2023
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 26, 2024
…cs_main

Summary:
This is a partial backport of [[bitcoin/bitcoin#26140 | core#26140]]
bitcoin/bitcoin@1d87137
Depends on D15287

Test Plan:
With Debug:

`ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15288
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 26, 2024
…c_mutex and move to Peer

Summary:
> nUnconnectingHeaders, m_headers_sync_timeout, fPreferHeaders and m_recently_announced_headers are currently all CNodeState members even though they are only ever accessed from the message processing thread (therefore sufficiently guarded exclusively by g_msgproc_mutex). CNodeState exists purely to hold validation-specific state guarded by cs_main that is accessed by multiple threads.

This is a partial backport of [[bitcoin/bitcoin#26140 | core#26140]]
bitcoin/bitcoin@5f80d8d
bitcoin/bitcoin@d8c0d1c
Depends on D15288

Test Plan:
With Debug:

`ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15289
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 26, 2024
…roc_mutex and move to Peer

Summary:
> nUnconnectingHeaders, m_headers_sync_timeout, fPreferHeaders and m_recently_announced_headers are currently all CNodeState members even though they are only ever accessed from the message processing thread (therefore sufficiently guarded exclusively by g_msgproc_mutex). CNodeState exists purely to hold validation-specific state guarded by cs_main that is accessed by multiple threads.

This is a partial backport of [[bitcoin/bitcoin#26140 | core#26140]]
bitcoin/bitcoin@4b84e50
bitcoin/bitcoin@4b84e50
Depends on D15289

Test Plan:
With Debug:

`ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15290
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 26, 2024
…x and move to Peer

Summary:
> nUnconnectingHeaders, m_headers_sync_timeout, fPreferHeaders and m_recently_announced_headers are currently all CNodeState members even though they are only ever accessed from the message processing thread (therefore sufficiently guarded exclusively by g_msgproc_mutex). CNodeState exists purely to hold validation-specific state guarded by cs_main that is accessed by multiple threads.

This is a partial backport of [[bitcoin/bitcoin#26140 | core#26140]]
bitcoin/bitcoin@3605011
bitcoin/bitcoin@8a2cb1f
Depends on D15290

Test Plan:
With Debug:

`ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15291
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 26, 2024
…ed by g_msgproc_mutex and move to Peer

Summary:
This is a partial backport of [[bitcoin/bitcoin#26140 | core#26140]]
bitcoin/bitcoin@938a8e2
bitcoin/bitcoin@279c53d
Depends on D15291

Test Plan:
With Debug

`ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15292
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 26, 2024
Summary:
```
-BEGIN VERIFY SCRIPT-
ren() { sed -i "s:\<$1\>:$2:g" $(git grep -l "\<$1\>" ./src ./test); }

ren nUnconnectingHeaders     m_num_unconnecting_headers_msgs
ren fPreferHeaders           m_prefers_headers
ren MAX_UNCONNECTING_HEADERS MAX_NUM_UNCONNECTING_HEADERS_MSGS

-END VERIFY SCRIPT
```

This concludes backport of [[bitcoin/bitcoin#26140 | core#26140]]
https://github.com/bitcoin/bitcoin/pull/26140/commits/3a060ae7b67cc28fc60cf28cbc518fa1df24f545-

Depends on D15292

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15293
@bitcoin bitcoin locked and limited conversation to collaborators Mar 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.