From 0b822cc6b258d37ec6011e1b6d10bb2542271628 Mon Sep 17 00:00:00 2001 From: DanS Date: Wed, 28 Oct 2026 12:32:54 -0500 Subject: [PATCH] wallet: keep a failed open of the wallet database inside the shutdown save SetBestChain opens the wallet database (CWalletDB) outside the try block of SetBestChainINTERNAL, and CDB's constructor throws when the environment or the file cannot be opened. Shutdown() calls the save with no handler around it, so such a failure ended in std::terminate: the coins views and the block tree were not deleted, the wallet was not flushed and the pidfile stayed. The hourly SetBestChain runs inside FlushStateToDisk's try block and never had this problem. PersistStateAtShutdown now catches the exception, logs the save as NOT saved with the reason, records nothing, and lets the teardown go on. Co-Authored-By: Claude Opus 5.5 --- src/gtest/test_wallet_shutdownsave.cpp | 39 ++++++++++++++++++++++++++ src/wallet/wallet.cpp | 13 +++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/gtest/test_wallet_shutdownsave.cpp b/src/gtest/test_wallet_shutdownsave.cpp index f5798011e..c5b55a916 100644 --- a/src/gtest/test_wallet_shutdownsave.cpp +++ b/src/gtest/test_wallet_shutdownsave.cpp @@ -6,8 +6,13 @@ #include +#include + #include "chain.h" +#include "chainparams.h" +#include "util.h" #include "validationinterface.h" +#include "wallet/db.h" #include "wallet/wallet.h" namespace { @@ -145,3 +150,37 @@ TEST_F(WalletShutdownSave, SkippedSaveWritesNothing) { ASSERT_NO_THROW(wallet.PersistStateAtShutdown(true)); ASSERT_NO_THROW(wallet.PersistStateAtShutdown(false)); } + +// Opening the wallet database can throw (an environment or file that cannot be opened). Shutdown() has no handler, +// so the save must catch it, record nothing, and return. +TEST_F(WalletShutdownSave, AFailedOpenDoesNotEscape) { + SelectParams(CBaseChainParams::MAIN); + const boost::filesystem::path dir = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("shutdownsave-%%%%%%%%"); + const bool hadDatadir = mapArgs.count("-datadir") > 0; + const std::string oldDatadir = hadDatadir ? mapArgs["-datadir"] : ""; + boost::filesystem::create_directories(dir); + mapArgs["-datadir"] = dir.string(); + ClearDatadirCache(); + // The environment's log directory is a regular file, so opening the environment throws. + { + FILE* f = fopen((GetDataDir() / "database").string().c_str(), "wb"); + ASSERT_TRUE(f != nullptr); + fclose(f); + } + + CWallet wallet("wallet.dat"); + uint256 hash = uint256S("0123"); + CBlockIndex tip; + tip.phashBlock = &hash; + wallet.SetWalletTip(&tip); + ASSERT_EQ("", Reason(wallet, true)); + { + LOCK(cs_main); + EXPECT_NO_THROW(wallet.PersistStateAtShutdown(true)); + } + EXPECT_EQ("", Reason(wallet, true)) << "a failed save must not be recorded as one"; + + if (hadDatadir) mapArgs["-datadir"] = oldDatadir; else mapArgs.erase("-datadir"); + ClearDatadirCache(); + boost::filesystem::remove_all(dir); +} diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 499dc5ed2..fc2aff3e7 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -909,9 +909,18 @@ void CWallet::PersistStateAtShutdown(bool fInitCompleted) const int nHeight = pindexWalletTip->GetHeight(); LogPrintf("Saving wallet state (note witnesses and best block %d, %u txs)...\n", nHeight, nTxs); int64_t nStart = GetTimeMillis(); - bool fWritten = SetBestChain(chainActive.GetLocator()); + bool fWritten = false; + std::string strFailure = "the write failed"; + // Opening the wallet database throws when the environment or the file cannot be opened. Shutdown() + // has no handler, so this must not escape: the rest of the teardown (the coins views, the wallet + // flush, the pidfile) still has to run. + try { + fWritten = SetBestChain(chainActive.GetLocator()); + } catch (const std::exception& e) { + strFailure = e.what(); + } LogPrintf("%s: wallet state %s at height %d: %u txs in %dms\n", __func__, - fWritten ? "saved" : "NOT saved (the write failed; the previous save stays on disk)", + fWritten ? "saved" : strprintf("NOT saved (%s; the previous save stays on disk)", strFailure), nHeight, nTxs, GetTimeMillis() - nStart); }