miner: take CreateNewBlock's locks with LOCK2 instead of by hand
CreateNewBlock entered cs_main and mempool.cs with ENTER_CRITICAL_SECTION and released them by hand before each of its four returns. Anything that threw in between -- TestBlockValidity, the notary payment, the coinbase setup, an allocation -- left both locks held by that thread for good: for the RPC thread in getblocktemplate that wedges every later cs_main user, and for a miner thread the same. The releases were also in the wrong order (cs_main before mempool.cs), which pops the wrong DEBUG_LOCKORDER lock-stack entry. Take them with LOCK2 for the block's scope, so every return and any exception releases both, in reverse order, and drop the four hand-written release pairs. The boost::this_thread::disable_interruption() before them constructed a temporary that was destroyed on the same line, so it never disabled anything. It is removed rather than "fixed": with scoped locks an interruption unwinds cleanly, and disabling it would only delay stopping a miner thread. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+6
-19
@@ -224,10 +224,12 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32
|
||||
|
||||
CBlockIndex* pindexPrev = 0;
|
||||
{
|
||||
// this should stop create block ever exiting until it has returned something.
|
||||
boost::this_thread::disable_interruption();
|
||||
ENTER_CRITICAL_SECTION(cs_main);
|
||||
ENTER_CRITICAL_SECTION(mempool.cs);
|
||||
// Scoped, so every return below and any exception releases both, in reverse order. They
|
||||
// were taken and released by hand, which leaked them whenever something in here threw, and
|
||||
// released cs_main before mempool.cs. (The boost::this_thread::disable_interruption() that
|
||||
// stood here built a temporary, so it never disabled anything; with scoped locks an
|
||||
// interruption now unwinds cleanly.)
|
||||
LOCK2(cs_main, mempool.cs);
|
||||
pindexPrev = chainActive.LastTip();
|
||||
const int nHeight = pindexPrev->GetHeight() + 1;
|
||||
const Consensus::Params &consensusParams = chainparams.GetConsensus();
|
||||
@@ -664,8 +666,6 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32
|
||||
}
|
||||
} else if ( (uint64_t)(txNew.vout[0].nValue) >= ASSETCHAINS_TIMELOCKGTE) {
|
||||
LogPrintf("timelocked chains not supported in this code!\n");
|
||||
LEAVE_CRITICAL_SECTION(cs_main);
|
||||
LEAVE_CRITICAL_SECTION(mempool.cs);
|
||||
return(0);
|
||||
} else if ( fNotarizationBlock && ASSETCHAINS_NOTARY_PAY[0] != 0 && pblock->vtx[1].vout.size() == 2 && pblock->vtx[1].vout[1].nValue == 0 ) {
|
||||
// Get the OP_RETURN for the notarization
|
||||
@@ -677,11 +677,6 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32
|
||||
if ( totalsats == 0 )
|
||||
{
|
||||
LogPrintf("Could not create notary payment, trying again.\n");
|
||||
// Release unconditionally to match the unconditional ENTER above. The old
|
||||
// `if(!isStake)` guard leaked cs_main/mempool.cs on the isStake path (this
|
||||
// still return(0)s), while the success and timelock paths always release.
|
||||
LEAVE_CRITICAL_SECTION(cs_main);
|
||||
LEAVE_CRITICAL_SECTION(mempool.cs);
|
||||
return(0);
|
||||
}
|
||||
} else LogPrintf("vout 2 of notarization is not OP_RETURN scriptlen.%i\n", scriptlen);
|
||||
@@ -726,11 +721,6 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32
|
||||
CValidationState state;
|
||||
if ( !TestBlockValidity(state, *pblock, pindexPrev, false, false)) // invokes CC checks
|
||||
{
|
||||
// Release unconditionally to match the unconditional ENTER above. The old
|
||||
// `if(!isStake)` guard leaked cs_main/mempool.cs on the isStake path (this
|
||||
// still return(0)s), while the success and timelock paths always release.
|
||||
LEAVE_CRITICAL_SECTION(cs_main);
|
||||
LEAVE_CRITICAL_SECTION(mempool.cs);
|
||||
LogPrintf("%s: TestBlockValidity failed!\n", __func__);
|
||||
//throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed"); // crashes the node, moved to GetBlockTemplate and issue return.
|
||||
return(0);
|
||||
@@ -738,9 +728,6 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32
|
||||
}
|
||||
}
|
||||
|
||||
LEAVE_CRITICAL_SECTION(cs_main);
|
||||
LEAVE_CRITICAL_SECTION(mempool.cs);
|
||||
|
||||
return pblocktemplate.release();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user