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) <noreply@anthropic.com>
This commit is contained in:
2026-11-26 22:26:29 -06:00
co-authored by Claude Opus 5.5
parent bc36fa5a74
commit 7d5893cd2a
+5
View File
@@ -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));