From: Azat Khuzhin Date: Tue, 22 Mar 2016 20:36:19 +0000 (+0300) Subject: http: avoid leaking of fd in evhttp_connection_free() X-Git-Tag: release-2.1.6-beta~52^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f0e13411795203c6a160dfc8d8f0fcb1913905ff;p=libevent 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. --- 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);