Skip to content

Commit

Permalink
http: Join worker threads before deleting work queue
Browse files Browse the repository at this point in the history
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 <laanwj@gmail.com>
  • Loading branch information
laanwj authored and furszy committed Feb 18, 2021
1 parent 7d68769 commit b8f7364
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ bool UpdateHTTPServerLogging(bool enable) {

std::thread threadHTTP;
std::future<bool> threadResult;
static std::vector<std::thread> g_thread_http_workers;

bool StartHTTPServer()
{
Expand All @@ -477,8 +478,7 @@ bool StartHTTPServer()
threadHTTP = std::thread(std::move(task), eventBase, eventHTTP);

for (int i = 0; i < rpcThreads; i++) {
std::thread rpc_worker(HTTPWorkQueueRun, workQueue);
rpc_worker.detach();
g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue);
}
return true;
}
Expand All @@ -502,6 +502,10 @@ void StopHTTPServer()
if (workQueue) {
LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
workQueue->WaitExit();
for (auto& thread: g_thread_http_workers) {
thread.join();
}
g_thread_http_workers.clear();
delete workQueue;
}
MilliSleep(500); // Avoid race condition while the last HTTP-thread is exiting
Expand Down

0 comments on commit b8f7364

Please sign in to comment.