Skip to content

Commit 660bdbf

Browse files
promagfjahr
authored andcommitted
http: Release server before waiting for event base loop exit
1 parent 8c6d007 commit 660bdbf

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/httpserver.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,18 @@ void StopHTTPServer()
486486
return g_requests.empty();
487487
});
488488
}
489-
if (eventBase) {
490-
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
491-
if (g_thread_http.joinable()) g_thread_http.join();
492-
}
493489
if (eventHTTP) {
494-
evhttp_free(eventHTTP);
495-
eventHTTP = nullptr;
490+
// Schedule a callback to call evhttp_free in the event base thread, so
491+
// that evhttp_free does not need to be called again after the handling
492+
// of unfinished request connections that follows.
493+
event_base_once(eventBase, -1, EV_TIMEOUT, [](evutil_socket_t, short, void*) {
494+
evhttp_free(eventHTTP);
495+
eventHTTP = nullptr;
496+
}, nullptr, nullptr);
496497
}
497498
if (eventBase) {
499+
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
500+
if (g_thread_http.joinable()) g_thread_http.join();
498501
event_base_free(eventBase);
499502
eventBase = nullptr;
500503
}

0 commit comments

Comments
 (0)