I added this to see if static analysis would catch any lock order inversions while investigating #34731, it did not catch any but it is a worthwhile defense against statically-analyzable inversions of cs_main->TxMempool::cs.
threads: qa: Add lock order annotation for `TxMempool::cs` #34813
pull davidgumberg wants to merge 1 commits into bitcoin:master from davidgumberg:2026-03-11-txmempoolcslockorder changing 1 files +1 −1-
davidgumberg commented at 2:02 AM on March 12, 2026: contributor
-
qa: Add lock order annotation for TxMempool::cs 9085dee476
- DrahtBot added the label Tests on Mar 12, 2026
-
DrahtBot commented at 2:03 AM on March 12, 2026: contributor
<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--021abf342d371248e50ceaed478a90ca-->
Reviews
See the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste <code><!--meta-tag:bot-skip--></code> into the comment that the bot should ignore.
<!--5faf32d7da4f0f540f40219e4f7537a3-->
- davidgumberg renamed this:
qa: Add lock order annotation for `TxMempool::cs`
thread: qa: Add lock order annotation for `TxMempool::cs`
on Mar 12, 2026 - davidgumberg renamed this:
thread: qa: Add lock order annotation for `TxMempool::cs`
threads: qa: Add lock order annotation for `TxMempool::cs`
on Mar 12, 2026 -
sedited commented at 7:47 AM on March 12, 2026: contributor
Concept ACK
I was surprised initially that triggering this warning seems to still require
-Wthread-safety-beta. Should we have been setting that flag at least in the CI somewhere? Looks like it is moved out of beta in the upcoming 23 release: https://github.com/llvm/llvm-project/pull/152853. -
fanquake commented at 9:54 AM on March 12, 2026: member
Looks like it is moved out of beta in the upcoming 23 release: https://github.com/llvm/llvm-project/pull/152853.
That change was part of LLVM 22, which is currently being used in the CI (#34660).
-
hebasto commented at 10:00 AM on March 12, 2026: member
Concept ACK.
-
sedited commented at 11:11 AM on March 12, 2026: contributor
That change was part of LLVM 22, which is currently being used in the CI (https://github.com/bitcoin/bitcoin/pull/34660).
Oh, should have checked the release notes properly, it is right there: https://releases.llvm.org/22.1.0/tools/clang/docs/ReleaseNotes.html#improvements-to-clang-s-diagnostics . Checked it manually in the meantime.
tested ACK 9085dee476d21daab786e88848597c755beecee4
- DrahtBot requested review from hebasto on Mar 12, 2026
- hebasto approved
-
hebasto commented at 11:29 AM on March 12, 2026: member
ACK 9085dee476d21daab786e88848597c755beecee4.
Tested on Ubuntu 25.10 using Clang 22.1.1 by applying the following patch:
--- a/src/txmempool.h +++ b/src/txmempool.h @@ -257,7 +257,7 @@ public: * changing the chain tip. It's necessary to keep both mutexes locked until * the mempool is consistent with the new chain tip and fully populated. */ - mutable RecursiveMutex cs ACQUIRED_AFTER(::cs_main); + mutable RecursiveMutex cs ACQUIRED_BEFORE(::cs_main); std::unique_ptr<TxGraph> m_txgraph GUARDED_BY(cs); mutable std::unique_ptr<TxGraph::BlockBuilder> m_builder GUARDED_BY(cs); indexed_transaction_set mapTx GUARDED_BY(cs);When using Clang 17.0.6, the annotation is ignored, as expected.
-
w0xlt commented at 6:34 PM on March 12, 2026: contributor
ACK 9085dee476d21daab786e88848597c755beecee4
Built with
cmake -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DAPPEND_CXXFLAGS="-Wthread-safety-beta"(Clang 18.1.3).Clean build, and verified the annotation catches wrong lock order when introducing a deliberate violation.
- fanquake merged this on Mar 13, 2026
- fanquake closed this on Mar 13, 2026