Skip to content

Commit

Permalink
Fix CCheckQueue IsIdle (potential) race condition and remove dangerou…
Browse files Browse the repository at this point in the history
…s constructors.

zcash: cherry picked from commit e207342
zcash: bitcoin/bitcoin#9497
  • Loading branch information
JeremyRubin authored and LarryRuane committed Apr 2, 2021
1 parent 80e66e7 commit 8e67b36
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/checkqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>

#include "sync.h"

template <typename T>
class CCheckQueueControl;

Expand Down Expand Up @@ -126,6 +128,9 @@ class CCheckQueue
}

public:
//! Mutex to ensure only one concurrent CCheckQueueControl
boost::mutex ControlMutex;

//! Create a new check queue
CCheckQueue(unsigned int nBatchSizeIn) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {}

Expand Down Expand Up @@ -160,12 +165,6 @@ class CCheckQueue
{
}

bool IsIdle()
{
boost::unique_lock<boost::mutex> lock(mutex);
return (nTotal == nIdle && nTodo == 0 && fAllOk == true);
}

};

/**
Expand All @@ -176,16 +175,18 @@ template <typename T>
class CCheckQueueControl
{
private:
CCheckQueue<T>* pqueue;
CCheckQueue<T> * const pqueue;
bool fDone;

public:
CCheckQueueControl(CCheckQueue<T>* pqueueIn) : pqueue(pqueueIn), fDone(false)
CCheckQueueControl() = delete;
CCheckQueueControl(const CCheckQueueControl&) = delete;
CCheckQueueControl& operator=(const CCheckQueueControl&) = delete;
explicit CCheckQueueControl(CCheckQueue<T> * const pqueueIn) : pqueue(pqueueIn), fDone(false)
{
// passed queue is supposed to be unused, or NULL
if (pqueue != NULL) {
bool isIdle = pqueue->IsIdle();
assert(isIdle);
ENTER_CRITICAL_SECTION(pqueue->ControlMutex);
}
}

Expand All @@ -208,6 +209,9 @@ class CCheckQueueControl
{
if (!fDone)
Wait();
if (pqueue != NULL) {
LEAVE_CRITICAL_SECTION(pqueue->ControlMutex);
}
}
};

Expand Down

0 comments on commit 8e67b36

Please sign in to comment.