Skip to content

Commit 97d2b09

Browse files
committed
Add helper to wait for validation interface queue to catch up
1 parent 3613749 commit 97d2b09

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/validation.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,11 +2572,7 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
25722572
// Block until the validation queue drains. This should largely
25732573
// never happen in normal operation, however may happen during
25742574
// reindex, causing memory blowup if we run too far ahead.
2575-
std::promise<void> promise;
2576-
CallFunctionInValidationInterfaceQueue([&promise] {
2577-
promise.set_value();
2578-
});
2579-
promise.get_future().wait();
2575+
SyncWithValidationInterfaceQueue();
25802576
}
25812577

25822578
if (ShutdownRequested())

src/validationinterface.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#include <sync.h>
1212
#include <txmempool.h>
1313
#include <util.h>
14+
#include <validation.h>
1415

1516
#include <list>
1617
#include <atomic>
18+
#include <future>
1719

1820
#include <boost/signals2/signal.hpp>
1921

@@ -118,6 +120,16 @@ void CallFunctionInValidationInterfaceQueue(std::function<void ()> func) {
118120
g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
119121
}
120122

123+
void SyncWithValidationInterfaceQueue() {
124+
AssertLockNotHeld(cs_main);
125+
// Block until the validation queue drains
126+
std::promise<void> promise;
127+
CallFunctionInValidationInterfaceQueue([&promise] {
128+
promise.set_value();
129+
});
130+
promise.get_future().wait();
131+
}
132+
121133
void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason reason) {
122134
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
123135
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {

src/validationinterface.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ void UnregisterAllValidationInterfaces();
4242
* will result in a deadlock (that DEBUG_LOCKORDER will miss).
4343
*/
4444
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
45+
/**
46+
* This is a synonym for the following, which asserts certain locks are not
47+
* held:
48+
* std::promise<void> promise;
49+
* CallFunctionInValidationInterfaceQueue([&promise] {
50+
* promise.set_value();
51+
* });
52+
* promise.get_future().wait();
53+
*/
54+
void SyncWithValidationInterfaceQueue();
4555

4656
class CValidationInterface {
4757
protected:

src/wallet/wallet.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,12 +1292,7 @@ void CWallet::BlockUntilSyncedToCurrentChain() {
12921292
// ...otherwise put a callback in the validation interface queue and wait
12931293
// for the queue to drain enough to execute it (indicating we are caught up
12941294
// at least with the time we entered this function).
1295-
1296-
std::promise<void> promise;
1297-
CallFunctionInValidationInterfaceQueue([&promise] {
1298-
promise.set_value();
1299-
});
1300-
promise.get_future().wait();
1295+
SyncWithValidationInterfaceQueue();
13011296
}
13021297

13031298

0 commit comments

Comments
 (0)