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 <laanwj@gmail.com>
pull/12366/head
Wladimir J. van der Laan 7 years ago
parent 1462bde767
commit b1c2370dde

@ -449,6 +449,7 @@ bool UpdateHTTPServerLogging(bool enable) {
std::thread threadHTTP; std::thread threadHTTP;
std::future<bool> threadResult; std::future<bool> threadResult;
static std::vector<std::thread> g_thread_http_workers;
bool StartHTTPServer() bool StartHTTPServer()
{ {
@ -460,8 +461,7 @@ bool StartHTTPServer()
threadHTTP = std::thread(std::move(task), eventBase, eventHTTP); threadHTTP = std::thread(std::move(task), eventBase, eventHTTP);
for (int i = 0; i < rpcThreads; i++) { for (int i = 0; i < rpcThreads; i++) {
std::thread rpc_worker(HTTPWorkQueueRun, workQueue); g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue);
rpc_worker.detach();
} }
return true; return true;
} }
@ -487,6 +487,10 @@ void StopHTTPServer()
if (workQueue) { if (workQueue) {
LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n"); LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
workQueue->WaitExit(); workQueue->WaitExit();
for (auto& thread: g_thread_http_workers) {
thread.join();
}
g_thread_http_workers.clear();
delete workQueue; delete workQueue;
workQueue = nullptr; workQueue = nullptr;
} }

Loading…
Cancel
Save