aboutsummaryrefslogtreecommitdiff
path: root/src/httpserver.cpp
diff options
context:
space:
mode:
authorstickies-v <stickies-v@protonmail.com>2023-09-29 15:24:14 +0100
committerstickies-v <stickies-v@protonmail.com>2023-10-03 13:35:46 +0100
commit68f23f57d77bc172ed39ecdd4d2d5cd5e13cf483 (patch)
tree7eb10fc4d1d47e81619d20d922b02dda217807e2 /src/httpserver.cpp
parent084d0372311e658a486622f720d2b827d8416591 (diff)
downloadbitcoin-68f23f57d77bc172ed39ecdd4d2d5cd5e13cf483.tar.xz
http: bugfix: track closed connection
It is possible that the client disconnects before the request is handled. In those cases, evhttp_request_set_on_complete_cb is never called, which means that on shutdown the server we'll keep waiting endlessly. By adding evhttp_connection_set_closecb, libevent automatically cleans up those dead connections at latest when we shutdown, and depending on the libevent version already at the moment of remote client disconnect. In both cases, the bug is fixed.
Diffstat (limited to 'src/httpserver.cpp')
-rw-r--r--src/httpserver.cpp5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/httpserver.cpp b/src/httpserver.cpp
index 995c446b58..069511563c 100644
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -262,19 +262,22 @@ std::string RequestMethodString(HTTPRequest::RequestMethod m)
/** HTTP request callback */
static void http_request_cb(struct evhttp_request* req, void* arg)
{
+ evhttp_connection* conn{evhttp_request_get_connection(req)};
// Track active requests
{
g_requests.AddRequest(req);
evhttp_request_set_on_complete_cb(req, [](struct evhttp_request* req, void*) {
g_requests.RemoveRequest(req);
}, nullptr);
+ evhttp_connection_set_closecb(conn, [](evhttp_connection* conn, void* arg) {
+ g_requests.RemoveConnection(conn);
+ }, nullptr);
}
// Disable reading to work around a libevent bug, fixed in 2.1.9
// See https://github.com/libevent/libevent/commit/5ff8eb26371c4dc56f384b2de35bea2d87814779
// and https://github.com/bitcoin/bitcoin/pull/11593.
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) {
- evhttp_connection* conn = evhttp_request_get_connection(req);
if (conn) {
bufferevent* bev = evhttp_connection_get_bufferevent(conn);
if (bev) {