Skip to content

Commit b396467

Browse files
committed
locks: Annotate CTxMemPool::check to require cs_main
Currently, CTxMemPool::check locks CTxMemPool's own cs member, then calls GetSpendHeight which locks cs_main. This can potentially cause an undesirable lock invesion since CTxMemPool's cs is supposed to be locked after cs_main. This does not cause us any problems right now because all callers of CTxMemPool already lock cs_main before calling CTxMemPool::check, which means that the LOCK(cs_main) in GetSpendHeight becomes benign. However, it is currently possible for new code to be added which calls CTxMemPool::check without locking cs_main (which would be dangerous). Therefore we should make it explicit that cs_main needs to be held before calling CTxMemPool::check. NOTE: After all review-only assertions are removed in "#20158 | tree-wide: De-globalize ChainstateManager", and assuming that we keep the changes in "validation: Pass in spendheight to CTxMemPool::check", we can re-evaluate to see if this annotation is still necessary.
1 parent 80486e7 commit b396467

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/txmempool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const
618618

619619
if (GetRand(m_check_ratio) >= 1) return;
620620

621+
AssertLockHeld(::cs_main);
621622
LOCK(cs);
622623
LogPrint(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
623624

src/txmempool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ class CTxMemPool
602602
* all inputs are in the mapNextTx array). If sanity-checking is turned off,
603603
* check does nothing.
604604
*/
605-
void check(const CCoinsViewCache *pcoins) const;
605+
void check(const CCoinsViewCache *pcoins) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
606606

607607
// addUnchecked must updated state for all ancestors of a given transaction,
608608
// to track size/count of descendant transactions. First version of

0 commit comments

Comments
 (0)