Skip to content

Commit 6a67366

Browse files
committed
Merge bitcoin/bitcoin#19033: http: Release work queue after event base finish
4e353cb http: Release work queue after event base finish (João Barbosa) Pull request description: This fixes a race between `http_request_cb` and `StopHTTPServer` where the work queue is used after release. Fixes #18856. ACKs for top commit: fjahr: Code review ACK 4e353cb achow101: ACK 4e353cb LarryRuane: ACK 4e353cb hebasto: ACK 4e353cb, tested (rebased on top of master 9313c4e) on Linux Mint 20.1 (x86_64) using MarcoFalke's [patch](bitcoin/bitcoin#19033 (comment)), including different `-rpcthreads`/`-rpcworkqueue` cases. The bug is fixed. The code is correct. Tree-SHA512: 185d2a9744d0d5134d782bf321ac9958ba17b11a5b3d70b4897c8243e6b146dfd3f23c57aef8e10ae9484374120b64389c1949a9cf0a21dccc47ffc934c20930
2 parents f6a25be + 4e353cb commit 6a67366

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
@@ -83,7 +83,7 @@ class WorkQueue
8383
bool Enqueue(WorkItem* item)
8484
{
8585
LOCK(cs);
86-
if (queue.size() >= maxDepth) {
86+
if (!running || queue.size() >= maxDepth) {
8787
return false;
8888
}
8989
queue.emplace_back(std::unique_ptr<WorkItem>(item));
@@ -99,7 +99,7 @@ class WorkQueue
9999
WAIT_LOCK(cs, lock);
100100
while (running && queue.empty())
101101
cond.wait(lock);
102-
if (!running)
102+
if (!running && queue.empty())
103103
break;
104104
i = std::move(queue.front());
105105
queue.pop_front();
@@ -448,8 +448,6 @@ void StopHTTPServer()
448448
thread.join();
449449
}
450450
g_thread_http_workers.clear();
451-
delete workQueue;
452-
workQueue = nullptr;
453451
}
454452
// Unlisten sockets, these are what make the event loop running, which means
455453
// that after this and all connections are closed the event loop will quit.
@@ -469,6 +467,10 @@ void StopHTTPServer()
469467
event_base_free(eventBase);
470468
eventBase = nullptr;
471469
}
470+
if (workQueue) {
471+
delete workQueue;
472+
workQueue = nullptr;
473+
}
472474
LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
473475
}
474476

0 commit comments

Comments
 (0)