Skip to content

Commit df9f712

Browse files
committed
Merge #13894: shutdown: Stop threads before resetting ptrs
faab631 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
2 parents 78dae8c + faab631 commit df9f712

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/init.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,7 @@ void Shutdown()
209209
// using the other before destroying them.
210210
if (peerLogic) UnregisterValidationInterface(peerLogic.get());
211211
if (g_connman) g_connman->Stop();
212-
peerLogic.reset();
213-
g_connman.reset();
214-
if (g_txindex) {
215-
g_txindex.reset();
216-
}
212+
if (g_txindex) g_txindex->Stop();
217213

218214
StopTorControl();
219215

@@ -222,6 +218,12 @@ void Shutdown()
222218
threadGroup.interrupt_all();
223219
threadGroup.join_all();
224220

221+
// After the threads that potentially access these pointers have been stopped,
222+
// destruct and reset all to nullptr.
223+
peerLogic.reset();
224+
g_connman.reset();
225+
g_txindex.reset();
226+
225227
if (g_is_mempool_loaded && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
226228
DumpMempool();
227229
}

0 commit comments

Comments
 (0)