diff --git a/src/init.cpp b/src/init.cpp index 7e296bebd..34403e3c1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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); diff --git a/src/main.cpp b/src/main.cpp index db0af063b..f71435fed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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); diff --git a/src/notarizationdb.cpp b/src/notarizationdb.cpp index 478d00583..26641cbaf 100644 --- a/src/notarizationdb.cpp +++ b/src/notarizationdb.cpp @@ -9,7 +9,10 @@ #include "main.h" #include -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;