Skip to content

Commit

Permalink
Hotfix: Fix infinite-delay in SynchMempool introduced in Fulcrum 1.9.4
Browse files Browse the repository at this point in the history
Oops. 1.9.4 was a lemon. SynchMempoolTask could hang forever in rare
cases, due to lost std::condition_variable notifications. This fixes the
situation.
  • Loading branch information
cculianu committed Nov 6, 2023
1 parent 4ab472e commit d88f816
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Controller_SynchMempoolTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <algorithm>
#include <cassert>
#include <chrono>
#include <condition_variable>
#include <exception>
#include <mutex>
Expand Down Expand Up @@ -123,7 +124,10 @@ void SynchMempoolTask::Precache::startThread(const size_t reserve, Mempool::TxHa
void SynchMempoolTask::Precache::stopThread()
{
if (thread.joinable()) {
stopFlag = true;
{
std::unique_lock g(mut);
stopFlag = true;
}
cond.notify_all();
thread.join();
}
Expand All @@ -136,7 +140,10 @@ void SynchMempoolTask::Precache::waitUntilDone()
{
if (thread.joinable()) {
Tic t0;
doneSubmittingWorkFlag = true;
{
std::unique_lock g(mut);
doneSubmittingWorkFlag = true;
}
cond.notify_all();
thread.join();
if (const double el = t0.msec<double>(); el >= 500.)
Expand Down Expand Up @@ -177,7 +184,7 @@ void SynchMempoolTask::Precache::threadFunc(const size_t reserve, const Mempool:
txns.clear();
{
std::unique_lock g(mut);
cond.wait(g, pred);
while ( ! cond.wait_for(g, std::chrono::seconds{1}, pred)) {} // paranoia: loop in a wait_for just in case future updates to code introduce lost cond signaling
txns.swap(workQueue);
if (stopFlag) return;
}
Expand Down

0 comments on commit d88f816

Please sign in to comment.