Skip to content

Commit 8c6d007

Browse files
promagfjahr
authored andcommitted
http: Track active requests and wait for last to finish
1 parent f3bc1a7 commit 8c6d007

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/httpserver.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@
2121
#include <util/threadnames.h>
2222
#include <util/translation.h>
2323

24+
#include <condition_variable>
2425
#include <cstdio>
2526
#include <cstdlib>
2627
#include <deque>
2728
#include <memory>
2829
#include <optional>
2930
#include <string>
31+
#include <unordered_set>
3032

3133
#include <sys/types.h>
3234
#include <sys/stat.h>
3335

3436
#include <event2/buffer.h>
3537
#include <event2/bufferevent.h>
3638
#include <event2/http.h>
39+
#include <event2/http_struct.h>
3740
#include <event2/keyvalq_struct.h>
3841
#include <event2/thread.h>
3942
#include <event2/util.h>
@@ -146,6 +149,10 @@ static GlobalMutex g_httppathhandlers_mutex;
146149
static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
147150
//! Bound listening sockets
148151
static std::vector<evhttp_bound_socket *> boundSockets;
152+
//! Track active requests
153+
static GlobalMutex g_requests_mutex;
154+
static std::condition_variable g_requests_cv;
155+
static std::unordered_set<evhttp_request*> g_requests GUARDED_BY(g_requests_mutex);
149156

150157
/** Check if a network address is allowed to access the HTTP server */
151158
static bool ClientAllowed(const CNetAddr& netaddr)
@@ -207,6 +214,17 @@ std::string RequestMethodString(HTTPRequest::RequestMethod m)
207214
/** HTTP request callback */
208215
static void http_request_cb(struct evhttp_request* req, void* arg)
209216
{
217+
// Track requests and notify when a request is completed.
218+
{
219+
WITH_LOCK(g_requests_mutex, g_requests.insert(req));
220+
g_requests_cv.notify_all();
221+
evhttp_request_set_on_complete_cb(req, [](struct evhttp_request* req, void*) {
222+
auto n{WITH_LOCK(g_requests_mutex, return g_requests.erase(req))};
223+
assert(n == 1);
224+
g_requests_cv.notify_all();
225+
}, nullptr);
226+
}
227+
210228
// Disable reading to work around a libevent bug, fixed in 2.2.0.
211229
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) {
212230
evhttp_connection* conn = evhttp_request_get_connection(req);
@@ -459,6 +477,15 @@ void StopHTTPServer()
459477
evhttp_del_accept_socket(eventHTTP, socket);
460478
}
461479
boundSockets.clear();
480+
{
481+
WAIT_LOCK(g_requests_mutex, lock);
482+
if (!g_requests.empty()) {
483+
LogPrint(BCLog::HTTP, "Waiting for %d requests to stop HTTP server\n", g_requests.size());
484+
}
485+
g_requests_cv.wait(lock, []() EXCLUSIVE_LOCKS_REQUIRED(g_requests_mutex) {
486+
return g_requests.empty();
487+
});
488+
}
462489
if (eventBase) {
463490
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
464491
if (g_thread_http.joinable()) g_thread_http.join();

0 commit comments

Comments
 (0)