diff --git a/src/init.cpp b/src/init.cpp index 50643ff96b..14dd8fc8ac 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1645,12 +1645,19 @@ bool AppInitMain() // Wait for genesis block to be processed { WaitableLock lock(cs_GenesisWait); - while (!fHaveGenesis) { - condvar_GenesisWait.wait(lock); + // We previously could hang here if StartShutdown() is called prior to + // ThreadImport getting started, so instead we just wait on a timer to + // check ShutdownRequested() regularly. + while (!fHaveGenesis && !ShutdownRequested()) { + condvar_GenesisWait.wait_for(lock, std::chrono::milliseconds(500)); } uiInterface.NotifyBlockTip.disconnect(BlockNotifyGenesisWait); } + if (ShutdownRequested()) { + return false; + } + // ********************************************************* Step 11: start node int chain_active_height; diff --git a/src/validation.cpp b/src/validation.cpp index 16b656c7f5..f1abe6f46d 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2581,9 +2581,6 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams& SyncWithValidationInterfaceQueue(); } - if (ShutdownRequested()) - break; - const CBlockIndex *pindexFork; bool fInitialDownload; { @@ -2630,6 +2627,13 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams& } if (nStopAtHeight && pindexNewTip && pindexNewTip->nHeight >= nStopAtHeight) StartShutdown(); + + // We check shutdown only after giving ActivateBestChainStep a chance to run once so that we + // never shutdown before connecting the genesis block during LoadChainTip(). Previously this + // caused an assert() failure during shutdown in such cases as the UTXO DB flushing checks + // that the best block hash is non-null. + if (ShutdownRequested()) + break; } while (pindexNewTip != pindexMostWork); CheckBlockIndex(chainparams.GetConsensus());