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 <noreply@anthropic.com>
This commit is contained in:
@@ -6,8 +6,13 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
+11
-2
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user