From 7d5893cd2a5d261b3b3c7356935b5c283e8693ce Mon Sep 17 00:00:00 2001 From: DanS Date: Thu, 26 Nov 2026 22:26:29 -0600 Subject: [PATCH] rpc: hold cs_main across gettxoutsetinfo's flush and scan gettxoutsetinfo flushed the chainstate and then scanned it with no lock. GetStats reads the best block and iterates a snapshot of the coins database separately, so a flush landing in between reported the UTXO totals of a different block than the "height" and "bestblock" it returned. Hold cs_main across both. The transparent UTXO set of a fully shielded chain is small, so the scan is short (12 outputs on a 113-block regtest chain). Co-Authored-By: Claude Opus 5.5 (1M context) --- src/rpc/blockchain.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 8dcd80635..36464159f 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -861,6 +861,11 @@ UniValue gettxoutsetinfo(const UniValue& params, bool fHelp, const CPubKey& mypk UniValue ret(UniValue::VOBJ); CCoinsStats stats; + // Hold cs_main across the flush and the scan: GetStats reads the best block and iterates a + // snapshot of the coins database separately, and a flush landing in between would report the + // UTXO totals of a different block than "height"/"bestblock" claim. The transparent UTXO set + // of a fully shielded chain is small, so the scan is short. + LOCK(cs_main); FlushStateToDisk(); if (pcoinsTip->GetStats(stats)) { ret.push_back(Pair("height", (int64_t)stats.nHeight));