Skip to content

Commit 4e79d3e

Browse files
committed
Ensure that event loop is empty before the loop exit
1 parent a90ca40 commit 4e79d3e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/httpserver.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,22 @@ void StopHTTPServer()
467467
}
468468
if (eventBase) {
469469
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
470-
// Exit the event loop as soon as there are no active events.
471-
event_base_loopexit(eventBase, nullptr);
470+
// Ensure that event loop is empty before the loop exit
471+
std::packaged_task<int(event_base*)> taskCleanup(event_base_dispatch);
472+
std::future<int> threadResultCleanup = taskCleanup.get_future();
473+
std::thread threadCleanup(std::move(taskCleanup), eventBase);
472474
// Give event loop a few seconds to exit (to send back last RPC responses), then break it
473475
// Before this was solved with event_base_loopexit, but that didn't work as expected in
474476
// at least libevent 2.0.21 and always introduced a delay. In libevent
475477
// master that appears to be solved, so in the future that solution
476478
// could be used again (if desirable).
477479
// (see discussion in https://github.com/bitcoin/bitcoin/pull/6990)
478-
if (threadResult.valid() && threadResult.wait_for(std::chrono::milliseconds(2000)) == std::future_status::timeout) {
480+
if ((threadResult.valid() && threadResult.wait_for(std::chrono::milliseconds(2000)) == std::future_status::timeout) ||
481+
(threadResultCleanup.valid() && threadResultCleanup.wait_for(std::chrono::milliseconds(2000)) == std::future_status::timeout)) {
479482
LogPrintf("HTTP event loop did not exit within allotted time, sending loopbreak\n");
480483
event_base_loopbreak(eventBase);
481484
}
485+
threadCleanup.join();
482486
threadHTTP.join();
483487
}
484488
if (eventHTTP) {

0 commit comments

Comments
 (0)