init: close the notarizations DB in Shutdown()

Shutdown() deleted pcoinsTip, pcoinsdbview and pblocktree but never
pnotarizations, so notarizations/LOCK was the one LevelDB lock still held
after "Shutdown: done". It was only released when the process was torn
down, after the static datadir .lock had already been released. A
successor started in that window took .lock, blocks/index and chainstate
and then failed to open notarizations/LOCK (EAGAIN), which the open
fallback "fixed" by moving the live notarizations DB aside and wiping it.

Delete it under cs_main next to pblocktree, and null the pointer after
the delete in the block-index load loop so a throwing reopen can never
leave it dangling for Shutdown() to delete twice.

Every user of pnotarizations now treats NULL as an empty DB
(GetBlockNotarizations, GetBackNotarization, ScanNotarizationsDB,
ConnectNotarizations, DisconnectNotarizations; crosschain.cpp,
rpc/crosschain.cpp, hush_nSPV_fullnode.h and hush_dailysnapshot only
reach it through those), so a straggler after Shutdown() cannot
dereference it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 10:23:20 -05:00
co-authored by Claude Opus 5.5
parent fe988d6fbe
commit f09f5a7b95
3 changed files with 22 additions and 1 deletions
+8
View File
@@ -274,6 +274,13 @@ void Shutdown()
delete pblocktree;
pblocktree = NULL;
}
// Close the notarizations DB with the other LevelDBs. It used to be left open until process
// teardown, which made notarizations/LOCK the one LevelDB lock still held after "Shutdown: done"
// (and after the datadir .lock was released), so a quickly restarted successor failed to open it.
if (pnotarizations != NULL) {
delete pnotarizations;
pnotarizations = NULL;
}
}
#ifdef ENABLE_WALLET
if (pwalletMain)
@@ -2080,6 +2087,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
delete pcoinscatcher;
delete pblocktree;
delete pnotarizations;
pnotarizations = NULL; // Shutdown() deletes it, so never leave it dangling if a reopen below throws
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex, dbCompression, dbMaxOpenFiles);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
+4
View File
@@ -2980,6 +2980,8 @@ static bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const CO
void ConnectNotarizations(const CBlock &block, int height)
{
if (pnotarizations == NULL) // closed by Shutdown()
return;
NotarizationsInBlock notarizations = ScanBlockNotarizations(block, height);
if (notarizations.size() > 0) {
CDBBatch batch = CDBBatch(*pnotarizations);
@@ -2992,6 +2994,8 @@ void ConnectNotarizations(const CBlock &block, int height)
void DisconnectNotarizations(const CBlock &block)
{
if (pnotarizations == NULL) // closed by Shutdown()
return;
NotarizationsInBlock nibs;
if (GetBlockNotarizations(block.GetHash(), nibs)) {
CDBBatch batch = CDBBatch(*pnotarizations);
+10 -1
View File
@@ -9,7 +9,10 @@
#include "main.h"
#include <boost/foreach.hpp>
NotarizationDB *pnotarizations;
// Owned by init: opened in AppInit2() and closed (set back to NULL) in Shutdown(), so that
// notarizations/LOCK is released together with the other LevelDB locks. Every accessor treats NULL
// as an empty DB, so a straggler after Shutdown() cannot dereference it.
NotarizationDB *pnotarizations = NULL;
NotarizationDB::NotarizationDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "notarizations", nCacheSize, fMemory, fWipe, false, 64) { }
NotarizationsInBlock ScanBlockNotarizations(const CBlock &block, int nHeight) {
@@ -55,11 +58,15 @@ NotarizationsInBlock ScanBlockNotarizations(const CBlock &block, int nHeight) {
}
bool GetBlockNotarizations(uint256 blockHash, NotarizationsInBlock &nibs) {
if (pnotarizations == NULL)
return false;
return pnotarizations->Read(blockHash, nibs);
}
bool GetBackNotarization(uint256 notarizationHash, Notarization &n) {
if (pnotarizations == NULL)
return false;
return pnotarizations->Read(notarizationHash, n);
}
@@ -86,6 +93,8 @@ void EraseBackNotarizations(const NotarizationsInBlock notarizations, CDBBatch &
// Scan notarizationsdb backwards for blocks containing a notarization
// for given symbol. Return height of matched notarization or 0.
int ScanNotarizationsDB(int height, std::string symbol, int scanLimitBlocks, Notarization& out) {
if (pnotarizations == NULL)
return 0;
if (height < 0 || height > chainActive.Height())
return false;