Skip to content

Commit b8f7364

Browse files
laanwjfurszy
authored andcommitted
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 bitcoin#12362. Signed-off-by: Wladimir J. van der Laan <[email protected]>
1 parent 7d68769 commit b8f7364

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
@@ -466,6 +466,7 @@ bool UpdateHTTPServerLogging(bool enable) {
466466

467467
std::thread threadHTTP;
468468
std::future<bool> threadResult;
469+
static std::vector<std::thread> g_thread_http_workers;
469470

470471
bool StartHTTPServer()
471472
{
@@ -477,8 +478,7 @@ bool StartHTTPServer()
477478
threadHTTP = std::thread(std::move(task), eventBase, eventHTTP);
478479

479480
for (int i = 0; i < rpcThreads; i++) {
480-
std::thread rpc_worker(HTTPWorkQueueRun, workQueue);
481-
rpc_worker.detach();
481+
g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue);
482482
}
483483
return true;
484484
}
@@ -502,6 +502,10 @@ void StopHTTPServer()
502502
if (workQueue) {
503503
LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
504504
workQueue->WaitExit();
505+
for (auto& thread: g_thread_http_workers) {
506+
thread.join();
507+
}
508+
g_thread_http_workers.clear();
505509
delete workQueue;
506510
}
507511
MilliSleep(500); // Avoid race condition while the last HTTP-thread is exiting

0 commit comments

Comments
 (0)