From 35b041ed142cedf48f8192d4192d6993c20d65f0 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 8 Aug 2018 14:02:54 +0200 Subject: [PATCH] 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 --- src/init.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 8dd9d1a9b3..4aeb624068 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -247,8 +247,7 @@ void PrepareShutdown() // 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->Stop(); //TODO watch out when backporting bitcoin#13033 (don't accidently put the reset here, as we've already backported bitcoin#13894 // After everything has been shut down, but before things get flushed, stop the // CScheduler/checkqueue threadGroup @@ -267,6 +266,12 @@ void PrepareShutdown() 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)) { DumpMempool(); }