Merge #13894: shutdown: Stop threads before resetting ptrs

faab63111d8f27335aa1f09c1a48da3be45135de shutdown: Stop threads before resetting ptrs (MarcoFalke)

Pull request description:

  On shutdown some threads would continue to run after or during a pointer reset. This leads to occasional segfaults on shutdown.

  Fix this by resetting the smart pointers after all threads that might read from them have been stopped.

  This should fix:
  * A segfault in the txindex thread, that occurs when the txindex destructor is done, but the thread was not yet stopped (as this is done in the base index destructor)
  * A segfault in the scheduler thread, which dereferences conman. (e.g. CheckForStaleTipAndEvictPeers)

Tree-SHA512: abbcf67fadd088e10fe8c384fadfb90bb115d5317145ccb5363603583b320efc18131e46384f55a9bc574969013dfcbd08c49e0d42c004ed7212eca193858ab2
This commit is contained in:
Wladimir J. van der Laan 2018-08-08 14:02:54 +02:00 committed by Alexander Block
parent c01e39d610
commit 35b041ed14

View File

@ -247,8 +247,7 @@ void PrepareShutdown()
// using the other before destroying them. // using the other before destroying them.
if (peerLogic) UnregisterValidationInterface(peerLogic.get()); if (peerLogic) UnregisterValidationInterface(peerLogic.get());
if (g_connman) g_connman->Stop(); if (g_connman) g_connman->Stop();
peerLogic.reset(); // if (g_txindex) g_txindex->Stop(); //TODO watch out when backporting bitcoin#13033 (don't accidently put the reset here, as we've already backported bitcoin#13894
g_connman.reset();
// After everything has been shut down, but before things get flushed, stop the // After everything has been shut down, but before things get flushed, stop the
// CScheduler/checkqueue threadGroup // CScheduler/checkqueue threadGroup
@ -267,6 +266,12 @@ void PrepareShutdown()
flatdb6.Dump(sporkManager); flatdb6.Dump(sporkManager);
} }
// After the threads that potentially access these pointers have been stopped,
// destruct and reset all to nullptr.
peerLogic.reset();
g_connman.reset();
//g_txindex.reset(); //TODO watch out when backporting bitcoin#13033 (don't accidently put the reset here, as we've already backported bitcoin#13894
if (fDumpMempoolLater && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) { if (fDumpMempoolLater && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool(); DumpMempool();
} }