threading: use a reverse lock rather than manual critsect macros
What changed, and why it matters
This commit is a small internal cleanup in Bitcoin Core's RPC mining code. It replaces manual lock/unlock macros with a safer, automatic 'reverse lock' helper that temporarily releases a lock and re-acquires it when done. The commit message explicitly says 'No functional change,' and the diff shows only a mechanical code-style swap with no behavior change visible.
No action needed. This is a non-functional refactoring. Treat as ordinary code maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
In src/rpc/mining.cpp’s getblocktemplate(), the old code used LOCK(cs_main), then LEAVE_CRITICAL_SECTION(cs_main) before a wait loop, and ENTER_CRITICAL_SECTION(cs_main) after. The new code uses WAIT_LOCK(cs_main, csmain_lock) and a scoped REVERSE_LOCK(csmain_lock, cs_main) around the wait loop. This is a RAII-style refactoring that removes manual lock management. There is no change to what is locked, when it is released, or what operations are performed while unlocked.
Changed components
src/rpc/mining.cppgetblocktemplate RPCInspect captured patch +2 / −4
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index 36adf154..b710c605 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -704,7 +704,7 @@ static RPCHelpMan getblocktemplate()
NodeContext& node = EnsureAnyNodeContext(request.context);
ChainstateManager& chainman = EnsureChainman(node);
Mining& miner = EnsureMining(node);
- LOCK(cs_main);
+ WAIT_LOCK(cs_main, csmain_lock);
uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};
std::string strMode = "template";
@@ -810,8 +810,8 @@ static RPCHelpMan getblocktemplate()
}
// Release lock while waiting
- LEAVE_CRITICAL_SECTION(cs_main);
{
+ REVERSE_LOCK(csmain_lock, cs_main);
MillisecondsDouble checktxtime{std::chrono::minutes(1)};
while (IsRPCRunning()) {
// If hashWatchedChain is not a real block hash, this will
@@ -830,8 +830,6 @@ static RPCHelpMan getblocktemplate()
checktxtime = std::chrono::seconds(10);
}
}
- ENTER_CRITICAL_SECTION(cs_main);
-
tip = CHECK_NONFATAL(miner.getTip()).value().hash;
if (!IsRPCRunning())
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.