From 4706cc5f600b389e7623972b0e6b5ba8482d6d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Tue, 28 May 2013 11:44:40 +0200 Subject: [PATCH] Fix filedescriptor/socket leaking --- src/main/stream.c | 8 ++++++-- src/main/stream_simple_socket.c | 3 ++- src/modules/fastcgi_stream.c | 8 ++++++-- src/modules/mod_openssl.c | 3 ++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/stream.c b/src/main/stream.c index fd94eab..4a0cd2b 100644 --- a/src/main/stream.c +++ b/src/main/stream.c @@ -297,6 +297,8 @@ liStream* li_stream_null_new(liEventLoop *loop) { static void iostream_destroy(liIOStream *iostream) { + int fd; + if (0 < iostream->stream_out.refcount || 0 < iostream->stream_in.refcount) return; iostream->stream_out.refcount = iostream->stream_in.refcount = 1; @@ -313,12 +315,14 @@ static void iostream_destroy(liIOStream *iostream) { iostream->write_timeout_queue = NULL; } + iostream->cb(iostream, LI_IOSTREAM_DESTROY); + + fd = li_event_io_fd(&iostream->io_watcher); + if (-1 != fd) close(fd); /* usually this should be shutdown+closed somewhere else */ li_event_clear(&iostream->io_watcher); li_iostream_throttle_clear(iostream); - iostream->cb(iostream, LI_IOSTREAM_DESTROY); - assert(1 == iostream->stream_out.refcount); assert(1 == iostream->stream_in.refcount); diff --git a/src/main/stream_simple_socket.c b/src/main/stream_simple_socket.c index c93e40a..9f01ee3 100644 --- a/src/main/stream_simple_socket.c +++ b/src/main/stream_simple_socket.c @@ -27,12 +27,13 @@ void li_stream_simple_socket_close(liIOStream *stream, gboolean aborted) { } } else { liWorker *wrk = li_worker_from_iostream(stream); - li_event_clear(&stream->io_watcher); + li_event_clear(&stream->io_watcher); /* sets io_fd to -1 */ shutdown(fd, SHUT_WR); li_stream_disconnect(&stream->stream_out); li_worker_add_closing_socket(wrk, fd); } + assert(-1 == li_event_io_fd(&stream->io_watcher)); } static void stream_simple_socket_read_throttle_notify(liThrottleState *state, gpointer data) { diff --git a/src/modules/fastcgi_stream.c b/src/modules/fastcgi_stream.c index c74fd46..c1bd805 100644 --- a/src/modules/fastcgi_stream.c +++ b/src/modules/fastcgi_stream.c @@ -196,8 +196,10 @@ static void backend_close(liBackendPool *bpool, liWorker *wrk, liBackendConnecti fcgi_debug("%s\n", "backend_close"); if (NULL != ctx->iostream) { + int fd; li_stream_simple_socket_close(ctx->iostream, FALSE); - li_iostream_reset(ctx->iostream); + fd = li_iostream_reset(ctx->iostream); + assert(-1 == fd); ctx->iostream = NULL; } li_stream_reset(&ctx->fcgi_in); @@ -267,6 +269,7 @@ static void fastcgi_reset(liFastCGIBackendContext *ctx) { if (!ctx->is_active) { li_backend_connection_closed(ctx->pool->public.subpool, ctx->subcon); } else { + int fd; const liFastCGIBackendCallbacks *callbacks = ctx->pool->callbacks; liFastCGIBackendConnection_p *currentcon = ctx->currentcon; liIOStream *iostream = ctx->iostream; @@ -276,7 +279,8 @@ static void fastcgi_reset(liFastCGIBackendContext *ctx) { ctx->request_done = TRUE; ctx->iostream = NULL; li_stream_simple_socket_close(iostream, TRUE); - li_iostream_reset(iostream); + fd = li_iostream_reset(iostream); + assert(-1 == fd); li_stream_disconnect(&ctx->fcgi_out); li_stream_disconnect_dest(&ctx->fcgi_in); diff --git a/src/modules/mod_openssl.c b/src/modules/mod_openssl.c index 00a557a..795e760 100644 --- a/src/modules/mod_openssl.c +++ b/src/modules/mod_openssl.c @@ -203,7 +203,8 @@ static gboolean openssl_con_new(liConnection *con, int fd) { if (NULL == conctx->ssl_filter) { ERROR(srv, "SSL_new: %s", ERR_error_string(ERR_get_error(), NULL)); - li_iostream_reset(conctx->sock_stream); + fd = li_iostream_reset(conctx->sock_stream); + close(fd); g_slice_free(openssl_connection_ctx, conctx); return FALSE; }