-
Notifications
You must be signed in to change notification settings - Fork 37.7k
validation, refactor: add missing thread safety lock assertions #24177
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
validation, refactor: add missing thread safety lock assertions #24177
Conversation
How to review: verify that a Clang debug build is clean (no For more information about Clang thread safety analysis, see |
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.
Concept ACK
I think it makes sense to add the missing thread-safety lock assertions, which prevents the risk of a race condition.
I shall take a thorough look at this PR before ACKing it. Meanwhile, I found some nit suggestions that should be addressed.
fe73d8f
to
8b5f841
Compare
Thanks @shaavan, updated with your feedback. |
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.
Concept ACK, it follows https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#threads-and-synchronization.
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.
I looked into both src/validation.cpp
and src/validation.h
, and I found a list of functions that are declared using EXCLUSIVE_LOCK_REQUIRED()
but does not have AssertLockHeld()
in their definition, and are also not yet addressed in this PR.
Declared in src/validation.cpp:
- LimitMempoolSize
[EXCLUSIVE_LOCKS_REQUIRED(pool.cs, ::cs_main)]
- CheckFeeRate
[EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)]
- AcceptToMemoryPool
[EXCLUSIVE_LOCKS_REQUIRED(cs_main)]
- ContextualCheckBlockHeader
[EXCLUSIVE_LOCKS_REQUIRED(cs_main)]
- PreChecks
[EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)]
- PolicyScriptChecks
[EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)]
- ConsensusScriptChecks
[EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)]
- Finalize
[EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_pool.cs)]
Declared in src/validation.h:
DisconnectTip[EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs)]
but onlyAssertLockHeld(cs_main)
- ProcessTransaction
[EXCLUSIVE_LOCKS_REQUIRED(cs_main)]
I think all of these functions come in the scope of this PR, and should be addressed in it.
Edit: DisconnectTip() has AssertLockHeld()
statement for both the mutex. So no need to make any changes to it. Thanks @jonatack for catching it.
Done, except for touching the
DisconnectTip()
conditional lock assertion onm_mempool->cs
8b5f841
to
1c0737e
Compare
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.
Btw I was curious about one thing. Why some functions have |
Good question. See #22932 (comment) |
So what I understood from #22932 (comment) is that |
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.
I found a minor nit that I didn't address in my previous review. Feel free to ignore, in case not updating the PR for another reason.
For new code, yes; for updated code, I suppose it depends on the PR author and on reviewer feedback. (Thanks for reviewing here!) |
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. 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. |
1c0737e
to
7ac8b46
Compare
Rebased and updated per |
7ac8b46
to
686d35d
Compare
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.
ACK 686d35d
ChainstateManager::Reset()
is called just from the destructor ChainstateManager::~ChainstateManager()
and from a test, but from the test the destructor is called shortly after. So, maybe the Reset()
method can be removed, its body copied to the destructor and the explicit call to Reset()
from the test removed? The only difference is that now, from the test, Reset()
is called first and UnloadBlockIndex()
is called afterwards (via the destructor). That order will be swapped, a cursory look shows that should be ok.
Thanks @vasild for having a look.
Done. It looks like |
686d35d
to
9a18bdb
Compare
Mind splitting the inline refactor from the annotations refactor into another pull? If not, that is fine, too. |
9a18bdb
to
acdead8
Compare
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.
ACK acdead8, I have reviewed the code (it was a bit tedious 😄) and it looks OK, I agree it can be merged.
Co-authored-by: Shashwat <svangani239@gmail.com>
acdead8
to
f485a07
Compare
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.
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.
ACK f485a07
…nager::Reset thread safety cleanups ae9ceed validation, refactoring: remove ChainstateManager::Reset() (Jon Atack) daad009 validation: replace lock with annotation in UnloadBlockIndex() (Jon Atack) Pull request description: Thread safety refactoring seen in #24177: - replace re-acquiring lock cs_main with a thread safety annotation in UnloadBlockIndex() - remove ChainstateManager::Reset(), as it is currently unused (can be reintroduced in the test utilities if needed for unit testing) ACKs for top commit: laanwj: Code review ACK ae9ceed vasild: ACK ae9ceed klementtan: crACK ae9ceed Tree-SHA512: cebb782572997cc2dda01590d6bb6c5e479e8202324d8b6ff459b814ce09e818b996c881736bfebd1b8bf4b6d7a0f79faf3ffea176a4699dd7d7429de2db2d13
Summary: Co-authored-by: Shashwat <svangani239@gmail.com> This is a backport of [[bitcoin/bitcoin#24177 | core#24177]] Some of this work was already done in D4581. Also add the lock annotation for CoinsDB which was missed in D7582 Test Plan: With DEBUG and TSAN: `ninja all check check-functional` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D12915
A number of functions in validation.{h,cpp} have a thread safety lock annotation in the declaration but are missing the corresponding run-time lock assertion in the definition.