Skip to content

Commit 3613749

Browse files
committed
Block ActivateBestChain to empty validationinterface queue
1 parent 5a933ce commit 3613749

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/test/test_bitcoin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
6969
fs::create_directories(pathTemp);
7070
gArgs.ForceSetArg("-datadir", pathTemp.string());
7171

72-
// Note that because we don't bother running a scheduler thread here,
73-
// callbacks via CValidationInterface are unreliable, but that's OK,
74-
// our unit tests aren't testing multiple parts of the code at once.
72+
// We have to run a scheduler thread to prevent ActivateBestChain
73+
// from blocking due to queue overrun.
74+
threadGroup.create_thread(boost::bind(&CScheduler::serviceQueue, &scheduler));
7575
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
7676

7777
mempool.setSanityCheck(1.0);

src/validation.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <validationinterface.h>
4141
#include <warnings.h>
4242

43+
#include <future>
4344
#include <sstream>
4445

4546
#include <boost/algorithm/string/replace.hpp>
@@ -2566,6 +2567,18 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
25662567
int nStopAtHeight = gArgs.GetArg("-stopatheight", DEFAULT_STOPATHEIGHT);
25672568
do {
25682569
boost::this_thread::interruption_point();
2570+
2571+
if (GetMainSignals().CallbacksPending() > 10) {
2572+
// Block until the validation queue drains. This should largely
2573+
// never happen in normal operation, however may happen during
2574+
// 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();
2580+
}
2581+
25692582
if (ShutdownRequested())
25702583
break;
25712584

0 commit comments

Comments
 (0)