Skip to content

Commit 68f23f5

Browse files
committed
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.
1 parent 084d037 commit 68f23f5

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/httpserver.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,22 @@ std::string RequestMethodString(HTTPRequest::RequestMethod m)
262262
/** HTTP request callback */
263263
static void http_request_cb(struct evhttp_request* req, void* arg)
264264
{
265+
evhttp_connection* conn{evhttp_request_get_connection(req)};
265266
// Track active requests
266267
{
267268
g_requests.AddRequest(req);
268269
evhttp_request_set_on_complete_cb(req, [](struct evhttp_request* req, void*) {
269270
g_requests.RemoveRequest(req);
270271
}, nullptr);
272+
evhttp_connection_set_closecb(conn, [](evhttp_connection* conn, void* arg) {
273+
g_requests.RemoveConnection(conn);
274+
}, nullptr);
271275
}
272276

273277
// Disable reading to work around a libevent bug, fixed in 2.1.9
274278
// See https://github.com/libevent/libevent/commit/5ff8eb26371c4dc56f384b2de35bea2d87814779
275279
// and https://github.com/bitcoin/bitcoin/pull/11593.
276280
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) {
277-
evhttp_connection* conn = evhttp_request_get_connection(req);
278281
if (conn) {
279282
bufferevent* bev = evhttp_connection_get_bufferevent(conn);
280283
if (bev) {

0 commit comments

Comments
 (0)