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