From 97d2b09c124e6e5803f7fd4503348d9710d1260f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 24 Dec 2017 12:13:13 -0500 Subject: [PATCH] Add helper to wait for validation interface queue to catch up --- src/validation.cpp | 6 +----- src/validationinterface.cpp | 12 ++++++++++++ src/validationinterface.h | 10 ++++++++++ src/wallet/wallet.cpp | 7 +------ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index 23ce6509d5a..1c024ce2aea 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2572,11 +2572,7 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams& // Block until the validation queue drains. This should largely // never happen in normal operation, however may happen during // reindex, causing memory blowup if we run too far ahead. - std::promise promise; - CallFunctionInValidationInterfaceQueue([&promise] { - promise.set_value(); - }); - promise.get_future().wait(); + SyncWithValidationInterfaceQueue(); } if (ShutdownRequested()) diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp index 1ddff4b335b..1e13465bf78 100644 --- a/src/validationinterface.cpp +++ b/src/validationinterface.cpp @@ -11,9 +11,11 @@ #include #include #include +#include #include #include +#include #include @@ -118,6 +120,16 @@ void CallFunctionInValidationInterfaceQueue(std::function func) { g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func)); } +void SyncWithValidationInterfaceQueue() { + AssertLockNotHeld(cs_main); + // Block until the validation queue drains + std::promise promise; + CallFunctionInValidationInterfaceQueue([&promise] { + promise.set_value(); + }); + promise.get_future().wait(); +} + void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason reason) { if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) { m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] { diff --git a/src/validationinterface.h b/src/validationinterface.h index 164059e3b9e..f416f6678f7 100644 --- a/src/validationinterface.h +++ b/src/validationinterface.h @@ -42,6 +42,16 @@ void UnregisterAllValidationInterfaces(); * will result in a deadlock (that DEBUG_LOCKORDER will miss). */ void CallFunctionInValidationInterfaceQueue(std::function func); +/** + * This is a synonym for the following, which asserts certain locks are not + * held: + * std::promise promise; + * CallFunctionInValidationInterfaceQueue([&promise] { + * promise.set_value(); + * }); + * promise.get_future().wait(); + */ +void SyncWithValidationInterfaceQueue(); class CValidationInterface { protected: diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index dafd708d09b..cf9bb2909fb 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1292,12 +1292,7 @@ void CWallet::BlockUntilSyncedToCurrentChain() { // ...otherwise put a callback in the validation interface queue and wait // for the queue to drain enough to execute it (indicating we are caught up // at least with the time we entered this function). - - std::promise promise; - CallFunctionInValidationInterfaceQueue([&promise] { - promise.set_value(); - }); - promise.get_future().wait(); + SyncWithValidationInterfaceQueue(); }