From 810bd6712faaffd9dda7db1f0fa8ca538a7fa4bb Mon Sep 17 00:00:00 2001 From: DanS Date: Wed, 8 Jul 2026 05:48:39 +0200 Subject: [PATCH] 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; correct the comment that incorrectly described the access as lock-free/race-free. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/main.cpp | 2 +- src/main.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5dc93c611..4fd905a04 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,7 +109,7 @@ bool fIsBareMultisigStd = true; bool fCheckBlockIndex = false; bool fCheckpointsEnabled = true; bool fCoinbaseEnforcedProtectionEnabled = true; -size_t nCoinCacheUsage = 5000 * 300; +std::atomic nCoinCacheUsage(5000 * 300); uint64_t nPruneTarget = 0; // If the tip is older than this (in seconds), the node is considered to be in initial block download. int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE; diff --git a/src/main.h b/src/main.h index 028d97b9a..26689de83 100644 --- a/src/main.h +++ b/src/main.h @@ -43,6 +43,7 @@ #include "txmempool.h" #include "uint256.h" +#include #include #include #include @@ -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 nCoinCacheUsage; extern CFeeRate minRelayTxFee; extern int64_t nMaxTipAge;