Skip to content

Commit

Permalink
Otherwise threads will try to continue modifying data structures whil…
Browse files Browse the repository at this point in the history
…e the dump process is being executed.

Flush CValidationInterface callbacks prior to destruction

Note that the CScheduler thread cant be running at this point,
it has already been stopped with the rest of the init threadgroup.
Thus, just calling any remaining loose callbacks during Shutdown()
is sane.
  • Loading branch information
TheBlueMatt authored and furszy committed Feb 14, 2021
1 parent 8022463 commit 64c525b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,19 @@ void PrepareShutdown()
fFeeEstimatesInitialized = false;
}

// FlushStateToDisk generates a SetBestChain callback, which we should avoid missing
FlushStateToDisk();

// After there are no more peers/RPC left to give us new data which may generate
// CValidationInterface callbacks, flush them...
GetMainSignals().FlushBackgroundCallbacks();

// Any future callbacks will be dropped. This should absolutely be safe - if
// missing a callback results in an unrecoverable situation, unclean shutdown
// would too. The only reason to do the above flushes is to let the wallet catch
// up with our current chain to avoid any strange pruning edge cases and make
// next startup faster by avoiding rescan.

{
LOCK(cs_main);
if (pcoinsTip != NULL) {
Expand Down
9 changes: 9 additions & 0 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,12 @@ void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void (void)>
}
MaybeScheduleProcessQueue();
}

void SingleThreadedSchedulerClient::EmptyQueue() {
bool should_continue = true;
while (should_continue) {
ProcessQueue();
LOCK(m_cs_callbacks_pending);
should_continue = !m_callbacks_pending.empty();
}
}
3 changes: 3 additions & 0 deletions src/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class SingleThreadedSchedulerClient {
public:
SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
void AddToProcessQueue(std::function<void (void)> func);

// Processes all remaining queue members on the calling thread, blocking until queue is empty
void EmptyQueue();
};

#endif
1 change: 1 addition & 0 deletions src/test/test_pivx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ TestingSetup::~TestingSetup()
UnregisterNodeSignals(GetNodeSignals());
threadGroup.interrupt_all();
threadGroup.join_all();
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
UnloadBlockIndex();
delete pcoinsTip;
Expand Down
4 changes: 4 additions & 0 deletions src/validationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ void CMainSignals::UnregisterBackgroundSignalScheduler() {
m_internals.reset(nullptr);
}

void CMainSignals::FlushBackgroundCallbacks() {
m_internals->m_schedulerClient.EmptyQueue();
}

CMainSignals& GetMainSignals()
{
return g_signals;
Expand Down
2 changes: 2 additions & 0 deletions src/validationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class CMainSignals {
void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
/** Unregister a CScheduler to give callbacks which should run in the background - these callbacks will now be dropped! */
void UnregisterBackgroundSignalScheduler();
/** Call any remaining callbacks on the calling thread */
void FlushBackgroundCallbacks();

void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
void TransactionAddedToMempool(const CTransactionRef &ptxn);
Expand Down

0 comments on commit 64c525b

Please sign in to comment.