Skip to content

Commit

Permalink
http: Release work queue after event base finish (btc#19033)
Browse files Browse the repository at this point in the history
backport of bitcoin#19033.

This fixes a race between http_request_cb and StopHTTPServer where
the work queue is used after release. - promag @ bitcoin.
  • Loading branch information
fdoving committed May 25, 2021
1 parent 4ffcf23 commit 2453af9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class WorkQueue
bool Enqueue(WorkItem* item)
{
std::unique_lock<std::mutex> lock(cs);
if (queue.size() >= maxDepth) {
if (!running || queue.size() >= maxDepth) {
return false;
}
queue.emplace_back(std::unique_ptr<WorkItem>(item));
Expand All @@ -127,7 +127,7 @@ class WorkQueue
std::unique_lock<std::mutex> lock(cs);
while (running && queue.empty())
cond.wait(lock);
if (!running)
if (!running && queue.empty())
break;
i = std::move(queue.front());
queue.pop_front();
Expand Down Expand Up @@ -505,6 +505,10 @@ void StopHTTPServer()
event_base_free(eventBase);
eventBase = nullptr;
}
if (workQueue) {
delete workQueue;
workQueue = nullptr;
}
LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
}

Expand Down

0 comments on commit 2453af9

Please sign in to comment.