Skip to content

Commit 343db5f

Browse files
knstUdjinM6PastaPastaPasta
authored
fix: double lock of deterministicMNManager->cs (dashpay#5637)
## Issue being fixed or feature implemented fix: double lock of deterministicMNManager->cs Logs: ``` node1 2023-10-21T16:46:03.990302Z (mocktime: 2014-12-04T17:33:19Z) [httpworker.1] DOUBLE LOCK DETECTED node1 2023-10-21T16:46:03.990322Z (mocktime: 2014-12-04T17:33:19Z) [httpworker.1] Lock order: node1 2023-10-21T16:46:03.990339Z (mocktime: 2014-12-04T17:33:19Z) [httpworker.1] 'cs_main' in miner.cpp:129 (in thread 'httpworker.1') node1 2023-10-21T16:46:03.990353Z (mocktime: 2014-12-04T17:33:19Z) [httpworker.1] 'm_mempool.cs' in miner.cpp:129 (in thread 'httpworker.1') node1 2023-10-21T16:46:03.990366Z (mocktime: 2014-12-04T17:33:19Z) [httpworker.1] (*) 'deterministicMNManager->cs' in evo/cbtx.cpp:114 (in thread 'httpworker.1') node1 2023-10-21T16:46:03.990439Z (mocktime: 2014-12-04T17:33:19Z) [httpworker.1] (*) 'cs' in ./evo/deterministicmns.h:614 (in thread 'httpworker.1') node1 2023-10-21T16:46:04.003619Z (mocktime: 2014-12-04T17:33:19Z) [httpworker.1] Posix Signal: Aborted No debug information available for stacktrace. You should add debug information and then run: dashd -printcrashinfo=bvcgc43iinzgc43ijfxgm3ybaacwiyltnbsbkudponuxqictnftw4ylmhiqecytpoj2gkzaphcbzaaaaaaaaayeurhimekiaaav6ldwqyiuqaafwsoe5bqrjaaahz6eh2dbcsaaakhhzaaaaaaaaanreseaaaaaaacgguliaaaaaaadyauwqaaaaaaacp3daaaaaaaaamxigcaaaaaaablpulyaaaaaaadovy3yaaaaaaahj7vbaaaaaaaadnbsdaaaaaaaaaa====== ``` Part of backtrace: ``` #9 UniqueLock<AnnotatedMixin<std::mutex>, std::unique_lock<std::mutex> >::UniqueLock (fTry=false, nLine=615, pszFile=0x55a9f71a3710 "./evo/deterministicmns.h", pszName=0x55a9f719caff "cs", mutexIn=..., this=0x7f7e1e71b250) at ./sync.h:164 #10 CDeterministicMNManager::GetListForBlock (this=0x55a9f84d06b0, pindex=0x7f7db03621c0) at ./evo/deterministicmns.h:615 #11 0x000055a9f6612258 in llmq::utils::ComputeQuorumMembersByQuarterRotation (pCycleQuorumBaseBlockIndex=0x7f7db03a6930, llmqParams=...) at /usr/include/c++/12/bits/unique_ptr.h:191 #12 llmq::utils::GetAllQuorumMembers (llmqType=<optimized out>, pQuorumBaseBlockIndex=0x7f7db0359bc0, reset_cache=reset_cache@entry=false) at llmq/utils.cpp:150 #13 0x000055a9f694d957 in CDeterministicMNManager::HandleQuorumCommitment (qc=..., pQuorumBaseBlockIndex=<optimized out>, mnList=..., debugLogs=debugLogs@entry=false) at evo/deterministicmns.cpp:989 #14 0x000055a9f695c455 in CDeterministicMNManager::BuildNewListFromBlock (this=<optimized out>, block=..., pindexPrev=pindexPrev@entry=0x7f7db03c1ac0, state=..., view=..., mnListRet=..., debugLogs=false) at evo/deterministicmns.cpp:918 #15 0x000055a9f692e7cd in CalcCbTxMerkleRootMNList (block=..., pindexPrev=pindexPrev@entry=0x7f7db03c1ac0, merkleRootRet=..., state=..., view=...) at /usr/include/c++/12/bits/unique_ptr.h:191 #16 0x000055a9f6a352ed in BlockAssembler::CreateNewBlock (this=this@entry=0x7f7e1e71f0b0, scriptPubKeyIn=...) at ./validation.h:649 #17 0x000055a9f6771c49 in generateBlocks (chainman=..., mempool=..., evodb=..., llmq_ctx=..., coinbase_script=..., nGenerate=10, nMaxTries=<optimized out>) at rpc/mining.cpp:167 #18 0x000055a9f677a496 in generatetoaddress (request=...) at /usr/include/c++/12/bits/unique_ptr.h:191 #19 0x000055a9f671ea02 in CRPCCommand::CRPCCommand(char const*, char const*, UniValue (*)(JSONRPCRequest const&), std::initializer_list<char const*>)::{lambda(JSONRPCRequest const&, UniValue&, bool)#1}::operator()(JSONRPCRequest const&, UniValue&, bool) const (__closure=<optimized out>, result=..., request=...) at ./rpc/server.h:120 ``` ## What was done? `CDeterministicMNManager::BuildNewListFromBlock` doesn't require `cs` lock anymore ## How Has This Been Tested? Run unit/functional tests ## Breaking Changes N/A ## Checklist: - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone --------- Co-authored-by: UdjinM6 <[email protected]> Co-authored-by: pasta <[email protected]>
1 parent c51cec6 commit 343db5f

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

src/evo/cbtx.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,10 @@ bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, const
111111

112112
bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev, uint256& merkleRootRet, BlockValidationState& state, const CCoinsViewCache& view)
113113
{
114-
LOCK(deterministicMNManager->cs);
115-
116114
try {
117-
static int64_t nTimeDMN = 0;
118-
static int64_t nTimeSMNL = 0;
119-
static int64_t nTimeMerkle = 0;
115+
static std::atomic<int64_t> nTimeDMN = 0;
116+
static std::atomic<int64_t> nTimeSMNL = 0;
117+
static std::atomic<int64_t> nTimeMerkle = 0;
120118

121119
int64_t nTime1 = GetTimeMicros();
122120

@@ -134,10 +132,12 @@ bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev
134132
int64_t nTime3 = GetTimeMicros(); nTimeSMNL += nTime3 - nTime2;
135133
LogPrint(BCLog::BENCHMARK, " - CSimplifiedMNList: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeSMNL * 0.000001);
136134

137-
static CSimplifiedMNList smlCached;
138-
static uint256 merkleRootCached;
139-
static bool mutatedCached{false};
135+
static Mutex cached_mutex;
136+
static CSimplifiedMNList smlCached GUARDED_BY(cached_mutex);
137+
static uint256 merkleRootCached GUARDED_BY(cached_mutex);
138+
static bool mutatedCached GUARDED_BY(cached_mutex) {false};
140139

140+
LOCK(cached_mutex);
141141
if (sml == smlCached) {
142142
merkleRootRet = merkleRootCached;
143143
if (mutatedCached) {

src/evo/deterministicmns.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, const CBlockInde
613613
int nHeight = pindex->nHeight;
614614

615615
try {
616-
LOCK(cs);
617-
618616
if (!BuildNewListFromBlock(block, pindex->pprev, state, view, newList, true)) {
619617
// pass the state returned by the function above
620618
return false;
@@ -626,6 +624,8 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, const CBlockInde
626624

627625
newList.SetBlockHash(pindex->GetBlockHash());
628626

627+
LOCK(cs);
628+
629629
oldList = GetListForBlockInternal(pindex->pprev);
630630
diff = oldList.BuildDiff(newList);
631631

@@ -708,11 +708,9 @@ void CDeterministicMNManager::UpdatedBlockTip(const CBlockIndex* pindex)
708708

709709
bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, BlockValidationState& state, const CCoinsViewCache& view, CDeterministicMNList& mnListRet, bool debugLogs)
710710
{
711-
AssertLockHeld(cs);
712-
713711
int nHeight = pindexPrev->nHeight + 1;
714712

715-
CDeterministicMNList oldList = GetListForBlockInternal(pindexPrev);
713+
CDeterministicMNList oldList = GetListForBlock(pindexPrev);
716714
CDeterministicMNList newList = oldList;
717715
newList.SetBlockHash(uint256()); // we can't know the final block hash, so better not return a (invalid) block hash
718716
newList.SetHeight(nHeight);

src/evo/deterministicmns.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,8 @@ class CDeterministicMNManager
574574
static constexpr int DISK_SNAPSHOTS = llmq_max_blocks() / DISK_SNAPSHOT_PERIOD + 1;
575575
static constexpr int LIST_DIFFS_CACHE_SIZE = DISK_SNAPSHOT_PERIOD * DISK_SNAPSHOTS;
576576

577-
public:
578-
Mutex cs;
579-
580577
private:
578+
Mutex cs;
581579
Mutex cs_cleanup;
582580
// We have performed CleanupCache() on this height.
583581
int did_cleanup GUARDED_BY(cs_cleanup) {0};
@@ -607,7 +605,7 @@ class CDeterministicMNManager
607605

608606
// the returned list will not contain the correct block hash (we can't know it yet as the coinbase TX is not updated yet)
609607
bool BuildNewListFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, BlockValidationState& state, const CCoinsViewCache& view,
610-
CDeterministicMNList& mnListRet, bool debugLogs) EXCLUSIVE_LOCKS_REQUIRED(cs);
608+
CDeterministicMNList& mnListRet, bool debugLogs) LOCKS_EXCLUDED(cs);
611609
static void HandleQuorumCommitment(const llmq::CFinalCommitment& qc, const CBlockIndex* pQuorumBaseBlockIndex, CDeterministicMNList& mnList, bool debugLogs);
612610

613611
CDeterministicMNList GetListForBlock(const CBlockIndex* pindex) LOCKS_EXCLUDED(cs) {

0 commit comments

Comments
 (0)