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
+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;