Skip to content

Commit 4e353cb

Browse files
committed
http: Release work queue after event base finish
This fixes a race between http_request_cb and StopHTTPServer where the work queue is used after release.
1 parent 2aaff48 commit 4e353cb

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/httpserver.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class WorkQueue
9191
bool Enqueue(WorkItem* item)
9292
{
9393
LOCK(cs);
94-
if (queue.size() >= maxDepth) {
94+
if (!running || queue.size() >= maxDepth) {
9595
return false;
9696
}
9797
queue.emplace_back(std::unique_ptr<WorkItem>(item));
@@ -107,7 +107,7 @@ class WorkQueue
107107
WAIT_LOCK(cs, lock);
108108
while (running && queue.empty())
109109
cond.wait(lock);
110-
if (!running)
110+
if (!running && queue.empty())
111111
break;
112112
i = std::move(queue.front());
113113
queue.pop_front();
@@ -456,8 +456,6 @@ void StopHTTPServer()
456456
thread.join();
457457
}
458458
g_thread_http_workers.clear();
459-
delete workQueue;
460-
workQueue = nullptr;
461459
}
462460
// Unlisten sockets, these are what make the event loop running, which means
463461
// that after this and all connections are closed the event loop will quit.
@@ -477,6 +475,10 @@ void StopHTTPServer()
477475
event_base_free(eventBase);
478476
eventBase = nullptr;
479477
}
478+
if (workQueue) {
479+
delete workQueue;
480+
workQueue = nullptr;
481+
}
480482
LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
481483
}
482484

0 commit comments

Comments
 (0)