diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2018-02-06 19:27:13 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2018-02-08 10:07:35 +0100 |
commit | dd346cb262c41461c689b4c6cd8bf2b5402ba9c3 (patch) | |
tree | 4a3aebb7c2a0596fda0efc5ff0ea7342daf1cd2b | |
parent | dafd0078a2b31370a155fa97d9b228439d312764 (diff) | |
download | bitcoin-dd346cb262c41461c689b4c6cd8bf2b5402ba9c3.tar.xz |
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>
Github-Pull: #12366
Rebased-From: b1c2370dde9ade180c638e5d9a4797f085322b5b
Tree-SHA512: 7fbe8e562ba0e1138f95deb42e84cda734bef0d7058349728b3e1602b9f65777c5b1be0f6bd5a7e513ac38487dc82e62fd0a6ee4ed985cbf36c90a7eec18eced
-rw-r--r-- | src/httpserver.cpp | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/httpserver.cpp b/src/httpserver.cpp index a022d220e0..a554dcb097 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -449,6 +449,7 @@ bool UpdateHTTPServerLogging(bool enable) { std::thread threadHTTP; std::future<bool> threadResult; +static std::vector<std::thread> g_thread_http_workers; bool StartHTTPServer() { @@ -460,8 +461,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; } @@ -487,6 +487,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; workQueue = nullptr; } |