Skip to content

Commit b1c2370

Browse files
committed
http: Join worker threads before deleting work queue
This prevents a potential race condition if control flow ends up in `ShutdownHTTPServer` before the thread gets to `queue->Run()`, deleting the work queue while workers are still going to use it. Meant to fix #12362. Signed-off-by: Wladimir J. van der Laan <[email protected]>
1 parent 1462bde commit b1c2370

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/httpserver.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ bool UpdateHTTPServerLogging(bool enable) {
449449

450450
std::thread threadHTTP;
451451
std::future<bool> threadResult;
452+
static std::vector<std::thread> g_thread_http_workers;
452453

453454
bool StartHTTPServer()
454455
{
@@ -460,8 +461,7 @@ bool StartHTTPServer()
460461
threadHTTP = std::thread(std::move(task), eventBase, eventHTTP);
461462

462463
for (int i = 0; i < rpcThreads; i++) {
463-
std::thread rpc_worker(HTTPWorkQueueRun, workQueue);
464-
rpc_worker.detach();
464+
g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue);
465465
}
466466
return true;
467467
}
@@ -487,6 +487,10 @@ void StopHTTPServer()
487487
if (workQueue) {
488488
LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
489489
workQueue->WaitExit();
490+
for (auto& thread: g_thread_http_workers) {
491+
thread.join();
492+
}
493+
g_thread_http_workers.clear();
490494
delete workQueue;
491495
workQueue = nullptr;
492496
}

0 commit comments

Comments
 (0)