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); }