Skip to content

Commit

Permalink
http: Release work queue after event base finish (btc#19033) (#38)
Browse files Browse the repository at this point in the history
* gui: encrypt wallet dialog, update to AVN (#33)

* http: Release work queue after event base finish (btc#19033)

backport of bitcoin#19033.

This fixes a race between http_request_cb and StopHTTPServer where
the work queue is used after release. - promag @ bitcoin.

(cherry picked from commit 994f5b4)
  • Loading branch information
fdoving authored and alamshafil committed Aug 22, 2022
1 parent aa4c2e5 commit 9b70c1f
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 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 @@ -478,8 +478,6 @@ void StopHTTPServer()
if (workQueue) {
LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
workQueue->WaitExit();
delete workQueue;
workQueue = nullptr;
}
if (eventBase) {
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
Expand All @@ -505,6 +503,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 9b70c1f

Please sign in to comment.