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 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 12:37:36 -05:00
co-authored by Claude Opus 5.5
parent 0b822cc6b2
commit 6413b8da4e
6 changed files with 34 additions and 16 deletions
+15 -5
View File
@@ -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";
+4 -2
View File
@@ -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) {
+3 -2
View File
@@ -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() {
+3 -2
View File
@@ -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();
+7 -3
View File
@@ -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;
+2 -2
View File
@@ -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