Skip to content
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

Additional thread safety annotations for CNode/Peer members accessed via the message processing thread #24474

Closed
wants to merge 10 commits into from

Conversation

ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Mar 4, 2022

Documents why the existing accesses of elements in net.h's CNode and net_processing.cpp's Peer objects is thread safe, generally by making it so that the compiler will complain if the non-safe accesses are added. Only leaves CNode's m_permissionFlags and m_prefer_evict unenforced by the compiler.

Adds a global mutex NetEventsInterface::g_mutex_msgproc_thread which is used to document the contract between net_processing and net that ProcessMessages and SendMessages are only invoked from a single thread, and which replaces the per-peer cs_sendProcessing mutex.

@DrahtBot DrahtBot added the P2P label Mar 4, 2022
@ajtowns
Copy link
Contributor Author

ajtowns commented Mar 4, 2022

I've pulled the m_mutex_message_handling change out of #21527 since it seems to have been the sticking point and refined it so that it is used to document all the un-annotated net/net_processing CNode/Peer variables in regards to thread-safety. The idea is this PR should just be converting the implicit assumptions we're already making to explicit ones.

@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 4, 2022

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25168 (refactor: Avoid passing params where not needed by MarcoFalke)
  • #24970 (net processing: Move cleanSubVer, fPreferredDownload and nLocalHostNonce to Peer by jnewbery)

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.

@ajtowns
Copy link
Contributor Author

ajtowns commented Mar 7, 2022

Rebased on top of #24427

@w0xlt
Copy link
Contributor

w0xlt commented Mar 16, 2022

Concept ACK.

@jonatack
Copy link
Member

Will have a look soon.

@ajtowns ajtowns force-pushed the 202203-msgproc-mutex branch from c5dd02f to 69df619 Compare March 25, 2022 16:41
@ajtowns
Copy link
Contributor Author

ajtowns commented Mar 25, 2022

Rebased past #21160

@ajtowns
Copy link
Contributor Author

ajtowns commented May 16, 2022

Rebased past #25109

ajtowns added 10 commits May 20, 2022 05:31
The (V1)TransportSerializer instance CNode::m_serializer is used from
multiple threads via PushMessage without protection by a mutex. This
is only thread safe because the class does not have any mutable state,
so document that by marking the methods and the object as "const".
Dereferencing a unique_ptr is not necessarily thread safe. The reason
these are safe is because their values are set at construction and do
not change later; so mark them as const and set them via the initializer
list to guarantee that.
m_permissionFlags and m_prefer_evict are treated as const -- they're
only set immediately after construction before any other thread has
access to the object, and not changed again afterwards. As such they
don't need to be marked atomic or guarded by a mutex; though it would
probably be better to actually mark them as const...
There are many cases where we asssume message processing is
single-threaded in order for how we access node-related memory to be
safe. Add an explicit mutex that we can use to document this, which allows
the compiler to catch any cases where we try to access that memory from
other threads and break that assumption.
SendMessages() is now protected g_mutex_msgproc_thread; so this additional
per-node mutex is redundant.
This allows CNode members to be marked as guarded by the
g_mutex_msgproc_thread mutex.
…proc thread

Previously vExtraTxnForCompact and vExtraTxnForCompactIt were protected
by g_cs_orphans; protect them by g_mutex_msgproc_thread instead, as they
are only used during message processing.
@ajtowns ajtowns force-pushed the 202203-msgproc-mutex branch from b710ddf to 49c3b28 Compare May 19, 2022 20:32
@ajtowns
Copy link
Contributor Author

ajtowns commented May 19, 2022

Rebased past #22778

@ajtowns
Copy link
Contributor Author

ajtowns commented May 20, 2022

See #25174 for the first few patches from this.

@DrahtBot
Copy link
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a "draft".

@ajtowns ajtowns changed the title Additional thread safety annotations for CNode/Peer Additional thread safety annotations for CNode/Peer members accessed via the message processing thread May 20, 2022
@ajtowns
Copy link
Contributor Author

ajtowns commented Aug 29, 2022

Closing this. If you'd like it resurrected please help #25174 make progress.

@ajtowns
Copy link
Contributor Author

ajtowns commented Sep 7, 2022

Replaced by #26036

@bitcoin bitcoin locked and limited conversation to collaborators Sep 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants