fix: make nCoinCacheUsage std::atomic to close an adaptive-dbcache data race

The scheduled AdjustCoinCacheForMemoryPressure task writes nCoinCacheUsage from
the scheduler thread holding no lock, while cs_main-holding threads
(FlushStateToDisk, VerifyDB) read it -- an unsynchronized read/write of a
non-atomic size_t (C++ UB). Make it std::atomic<size_t>; correct the comment
that incorrectly described the access as lock-free/race-free.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-08 05:48:39 +02:00
parent bf5b066a8d
commit 810bd6712f
2 changed files with 3 additions and 2 deletions

View File

@@ -43,6 +43,7 @@
#include "txmempool.h"
#include "uint256.h"
#include <atomic>
#include <algorithm>
#include <exception>
#include <map>
@@ -189,7 +190,7 @@ extern bool fCheckpointsEnabled;
// TODO: remove this flag by structuring our code such that
// it is unneeded for testing
extern bool fCoinbaseEnforcedProtectionEnabled;
extern size_t nCoinCacheUsage;
extern std::atomic<size_t> nCoinCacheUsage;
extern CFeeRate minRelayTxFee;
extern int64_t nMaxTipAge;