]> granicus.if.org Git - libevent/commitdiff
http: avoid leaking of fd in evhttp_connection_free()
authorAzat Khuzhin <a3at.mail@gmail.com>
Tue, 22 Mar 2016 20:36:19 +0000 (23:36 +0300)
committerAzat Khuzhin <a3at.mail@gmail.com>
Wed, 23 Mar 2016 09:53:51 +0000 (12:53 +0300)
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

diff --git a/http.c b/http.c
index d95cf1df846345cd550b917bd51dcdd05bab467e..a12d77316dc3dff6457a85dc2fa229454def0a39 100644 (file)
--- 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);