From f0e13411795203c6a160dfc8d8f0fcb1913905ff Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 22 Mar 2016 23:36:19 +0300 Subject: [PATCH] http: avoid leaking of fd in evhttp_connection_free() Since we do close fd there if we don't have BEV_OPT_CLOSE_ON_FREE, and evcon->fd can be incorrect (non -1), so just get it from the underlying bufferevent to fix this. And after this patch the following tests report 0 instead of 2307 fd leaks: $ valgrind --leak-check=full --show-reachable=yes --track-fds=yes --error-exitcode=1 regress --no-fork http/cancel.. ==11299== FILE DESCRIPTORS: 3 open at exit. And this is stdin/stderr/stdout. --- http.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/http.c b/http.c index d95cf1df..a12d7731 100644 --- a/http.c +++ b/http.c @@ -1244,6 +1244,9 @@ evhttp_connection_free(struct evhttp_connection *evcon) event_deferred_cb_cancel_(get_deferred_queue(evcon), &evcon->read_more_deferred_cb); + if (evcon->fd == -1) + evcon->fd = bufferevent_getfd(evcon->bufev); + if (evcon->fd != -1) { bufferevent_disable(evcon->bufev, EV_READ|EV_WRITE); shutdown(evcon->fd, EVUTIL_SHUT_WR);