Merge #13894: shutdown: Stop threads before resetting ptrs

faab63111d 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
commit df9f712746
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
1 changed files with 7 additions and 5 deletions

View File

@ -209,11 +209,7 @@ void Shutdown()
// using the other before destroying them.
if (peerLogic) UnregisterValidationInterface(peerLogic.get());
if (g_connman) g_connman->Stop();
peerLogic.reset();
g_connman.reset();
if (g_txindex) {
g_txindex.reset();
}
if (g_txindex) g_txindex->Stop();
StopTorControl();
@ -222,6 +218,12 @@ void Shutdown()
threadGroup.interrupt_all();
threadGroup.join_all();
// 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();
if (g_is_mempool_loaded && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool();
}