From 6413b8da4e2d03ef8b5b8e7532420a5664b3de47 Mon Sep 17 00:00:00 2001 From: DanS Date: Wed, 28 Oct 2026 12:37:36 -0500 Subject: [PATCH] wallet: skip the shutdown save when the chainstate flush failed The shutdown save runs right after FlushStateToDisk(), whose void wrapper dropped the result. If that flush failed (out of disk space, a coins database or block index write error), the save still stamped the wallet tip, a block the chainstate and block index on disk might not have, and started a write the size of the wallet on what is typically a full disk. FlushStateToDisk() now returns the result of the flush it runs (true for an nSPV light client, which flushes nothing). Its behaviour is unchanged, and the other callers ignore the result as before. Shutdown() passes it to PersistStateAtShutdown, which skips the save with "the chainstate flush failed". Co-Authored-By: Claude Opus 5.5 --- src/gtest/test_wallet_shutdownsave.cpp | 20 +++++++++++++++----- src/init.cpp | 6 ++++-- src/main.cpp | 5 +++-- src/main.h | 5 +++-- src/wallet/wallet.cpp | 10 +++++++--- src/wallet/wallet.h | 4 ++-- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/gtest/test_wallet_shutdownsave.cpp b/src/gtest/test_wallet_shutdownsave.cpp index c5b55a916..78dda74ba 100644 --- a/src/gtest/test_wallet_shutdownsave.cpp +++ b/src/gtest/test_wallet_shutdownsave.cpp @@ -17,9 +17,9 @@ namespace { -std::string Reason(CWallet& wallet, bool fInitCompleted) +std::string Reason(CWallet& wallet, bool fInitCompleted, bool fChainstateFlushed = true) { - const char* reason = wallet.ShutdownSaveSkipReason(fInitCompleted); + const char* reason = wallet.ShutdownSaveSkipReason(fInitCompleted, fChainstateFlushed); return reason ? reason : ""; } @@ -58,6 +58,16 @@ TEST_F(WalletShutdownSave, SkipsAfterAWalletFailure) { EXPECT_EQ("", Reason(wallet, true)); } +// A failed chainstate flush (typically a full disk) leaves the block the locator would name possibly missing from the +// chainstate on disk. +TEST_F(WalletShutdownSave, SkipsWhenTheChainstateFlushFailed) { + CWallet wallet; + CBlockIndex tip; + wallet.SetWalletTip(&tip); + EXPECT_EQ("the chainstate flush failed", Reason(wallet, true, false)); + EXPECT_EQ("", Reason(wallet, true, true)); +} + // Once a save (or init, finding nothing to rescan) has recorded the wallet's block, a shutdown writes again // only if the wallet moved to another block or its note state changed. TEST_F(WalletShutdownSave, SkipsWhenNothingChangedSinceTheLastSave) { @@ -147,8 +157,8 @@ TEST_F(WalletShutdownSave, SkippedSaveWritesNothing) { wallet.SetWalletTip(&tip); wallet.MarkStatePersisted(&tip); LOCK(cs_main); - ASSERT_NO_THROW(wallet.PersistStateAtShutdown(true)); - ASSERT_NO_THROW(wallet.PersistStateAtShutdown(false)); + ASSERT_NO_THROW(wallet.PersistStateAtShutdown(true, true)); + ASSERT_NO_THROW(wallet.PersistStateAtShutdown(false, true)); } // Opening the wallet database can throw (an environment or file that cannot be opened). Shutdown() has no handler, @@ -176,7 +186,7 @@ TEST_F(WalletShutdownSave, AFailedOpenDoesNotEscape) { ASSERT_EQ("", Reason(wallet, true)); { LOCK(cs_main); - EXPECT_NO_THROW(wallet.PersistStateAtShutdown(true)); + EXPECT_NO_THROW(wallet.PersistStateAtShutdown(true, true)); } EXPECT_EQ("", Reason(wallet, true)) << "a failed save must not be recorded as one"; diff --git a/src/init.cpp b/src/init.cpp index 452e53988..2f2a682dc 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -259,12 +259,14 @@ void Shutdown() { LOCK(cs_main); if (pcoinsTip != NULL) { - FlushStateToDisk(); + bool fChainstateFlushed = FlushStateToDisk(); #ifdef ENABLE_WALLET // The hourly SetBestChain in FlushStateToDisk rarely fires here, so a session shorter // than an hour would otherwise never save its witnesses and best block. if (pwalletMain) - pwalletMain->PersistStateAtShutdown(fInitCompleted); + pwalletMain->PersistStateAtShutdown(fInitCompleted, fChainstateFlushed); +#else + (void)fChainstateFlushed; #endif } if (pcoinsTip != NULL) { diff --git a/src/main.cpp b/src/main.cpp index db0af063b..7593a43b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3846,10 +3846,11 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) { return true; } -void FlushStateToDisk() { +bool FlushStateToDisk() { CValidationState state; if ( HUSH_NSPV_FULLNODE ) - FlushStateToDisk(state, FLUSH_STATE_ALWAYS); + return FlushStateToDisk(state, FLUSH_STATE_ALWAYS); + return true; } void PruneAndFlush() { diff --git a/src/main.h b/src/main.h index 89e962497..861ff7d13 100644 --- a/src/main.h +++ b/src/main.h @@ -323,8 +323,9 @@ CBlockIndex * InsertBlockIndex(uint256 hash); bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats); /** Increase a node's misbehavior score. */ void Misbehaving(NodeId nodeid, int howmuch); -/** Flush all state, indexes and buffers to disk. */ -void FlushStateToDisk(); +/** Flush all state, indexes and buffers to disk. False if the flush failed (out of disk space, a write + * error); true also for an nSPV light client, which has no chainstate to flush. */ +bool FlushStateToDisk(); /** Prune block files and flush state to disk. */ void PruneAndFlush(); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index fc2aff3e7..430a5c729 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -867,7 +867,7 @@ void CWallet::MarkStatePersisted(const CBlockIndex* pindex) RecordStatePersisted(pindex, nWitnessGeneration); } -const char* CWallet::ShutdownSaveSkipReason(bool fInitCompleted) +const char* CWallet::ShutdownSaveSkipReason(bool fInitCompleted, bool fChainstateFlushed) { // AppInit2 failed or was cut short: the other threads were interrupted but not joined, and the // wallet tip may still be unset, in which case SetBestChain would stamp the chain tip over an @@ -876,6 +876,10 @@ const char* CWallet::ShutdownSaveSkipReason(bool fInitCompleted) return "startup did not complete"; if (fWalletNotifyFailed) return "the wallet failed to process a block"; + // The locator would then name a block the chainstate and block index on disk may not have, and + // this is typically a full disk, where a write the size of the wallet is the worst thing to start. + if (!fChainstateFlushed) + return "the chainstate flush failed"; if (!bitdb.IsHealthy()) return "the wallet database environment reported an error"; LOCK(cs_wallet); @@ -891,10 +895,10 @@ const char* CWallet::ShutdownSaveSkipReason(bool fInitCompleted) return nullptr; } -void CWallet::PersistStateAtShutdown(bool fInitCompleted) +void CWallet::PersistStateAtShutdown(bool fInitCompleted, bool fChainstateFlushed) { LOCK2(cs_main, cs_wallet); - const char* skip = ShutdownSaveSkipReason(fInitCompleted); + const char* skip = ShutdownSaveSkipReason(fInitCompleted, fChainstateFlushed); if (skip != nullptr) { LogPrintf("%s: not saving the wallet state: %s\n", __func__, skip); return; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 5b4f8e4db..ac614351b 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1341,12 +1341,12 @@ public: * was last saved, so a shutdown with no changes after this has nothing to write. */ void MarkStatePersisted(const CBlockIndex* pindex); /** Why a clean shutdown must not write the wallet state now, or nullptr if it should. */ - const char* ShutdownSaveSkipReason(bool fInitCompleted); + const char* ShutdownSaveSkipReason(bool fInitCompleted, bool fChainstateFlushed); /** Called by Shutdown() under cs_main after the chainstate flush, with every other thread * stopped: writes the witnesses and the best-block locator (SetBestChain), unless a gate in * ShutdownSaveSkipReason says not to. Without it a session shorter than the hourly write never * saves, and the next start rescans and re-extends every witness across the whole session. */ - void PersistStateAtShutdown(bool fInitCompleted); + void PersistStateAtShutdown(bool fInitCompleted, bool fChainstateFlushed); /** Rescan bookkeeping for the shutdown save. A scan reports its start with the height of the * locator on disk (getting back the height an earlier interrupted scan left, or -1), where it * stopped if cut short (the height it left the locator at), and its start once it reached the