Skip to content

Commit 91d646d

Browse files
PastaPastaPastaknst
authored andcommitted
Merge dashpay#6787: fix: resolve race condition in quorum data recovery thread management
0e2811f fix: resolve race condition in quorum data recovery thread management (pasta) Pull request description: ## Summary This PR fixes a thread safety issue in the quorum data recovery system where multiple threads could be started for the same quorum due to a race condition. ### Problem The original code used a check-then-set pattern: ```cpp if (pQuorum->fQuorumDataRecoveryThreadRunning) { // Check return; } pQuorum->fQuorumDataRecoveryThreadRunning = true; // Set ``` Even though `fQuorumDataRecoveryThreadRunning` is declared as `std::atomic<bool>`, this pattern creates a race condition window where multiple threads can pass the check before any of them sets the flag. ### Solution Replace the check-then-set pattern with an atomic `compare_exchange_strong` operation: ```cpp bool expected = false; if (\!pQuorum->fQuorumDataRecoveryThreadRunning.compare_exchange_strong(expected, true)) { return; } ``` This ensures thread-safe access by atomically checking the current value and setting it to `true` only if it was previously `false`. ### Impact - Prevents multiple data recovery threads from being started for the same quorum - Eliminates potential resource conflicts and duplicate operations - Maintains the same functional behavior while ensuring thread safety ## Test plan - [x] Code compiles successfully - [x] Change maintains existing API and functionality - [x] Race condition eliminated through atomic operation Generated with [Claude Code](https://claude.ai/code) ACKs for top commit: UdjinM6: utACK 0e2811f Tree-SHA512: eff2798d535ba10d3baacb4b8aab731b6b0090d5f05c77c98beee07d116221184684d27bcacdbbf3cd8f63af952464dff2e7e2737c9c4c19f9fadef92424be81
1 parent fa4a4c6 commit 91d646d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/llmq/quorums.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,11 @@ void CQuorumManager::StartQuorumDataRecoveryThread(CConnman& connman, const CQuo
924924
{
925925
assert(m_mn_activeman);
926926

927-
if (pQuorum->fQuorumDataRecoveryThreadRunning) {
927+
bool expected = false;
928+
if (!pQuorum->fQuorumDataRecoveryThreadRunning.compare_exchange_strong(expected, true)) {
928929
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Already running\n", __func__);
929930
return;
930931
}
931-
pQuorum->fQuorumDataRecoveryThreadRunning = true;
932932

933933
workerPool.push([&connman, pQuorum, pIndex, nDataMaskIn, this](int threadId) {
934934
size_t nTries{0};

0 commit comments

Comments
 (0)