]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MAJOR: lua/socket: resources not detroyed when the socket is aborted
authorThierry FOURNIER <thierry.fournier@ozon.io>
Sun, 16 Jul 2017 18:48:54 +0000 (20:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 18 Jul 2017 04:41:33 +0000 (06:41 +0200)
In some cases, the socket is misused. The user can open socket and never
close it, or open the socket and close it without sending data. This
causes resources leak on all resources associated to the stream (buffer,
spoe, ...)

This is caused by the stream_shutdown function which is called outside
of the stream execution process. Sometimes, the shtudown is required
while the stream is not started, so the cleanup is ignored.

This patch change the shutdown mode of the session. Now if the session is
no longer used and the Lua want to destroy it, it just set a destroy flag
and the session kill itself.

This patch should be backported in 1.6 and 1.7

include/types/applet.h
src/hlua.c

index 5c1b074d3ea72ac72583b3fa0759d7c1acfe1251..e81cff71abfd6e79666047b14557a9559859ec09 100644 (file)
@@ -76,6 +76,7 @@ struct appctx {
                        struct hlua_socket *socket;
                        struct list wake_on_read;
                        struct list wake_on_write;
+                       int die;
                } hlua_cosocket;                /* used by the Lua cosockets */
                struct {
                        struct hlua *hlua;
index de4af39e1f166f9c6e47fc9146b61aa847df9e35..ef172fb68d997827bd30f8eee73b4f1596914948 100644 (file)
@@ -1553,6 +1553,15 @@ static void hlua_socket_handler(struct appctx *appctx)
        struct stream_interface *si = appctx->owner;
        struct connection *c = objt_conn(si_opposite(si)->end);
 
+       if (appctx->ctx.hlua_cosocket.die) {
+               si_shutw(si);
+               si_shutr(si);
+               si_ic(si)->flags |= CF_READ_NULL;
+               hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
+               hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
+               stream_shutdown(si_strm(si), SF_ERR_KILLED);
+       }
+
        /* If the connection object is not avalaible, close all the
         * streams and wakeup everithing waiting for.
         */
@@ -1628,9 +1637,10 @@ __LJMP static int hlua_socket_gc(lua_State *L)
 
        /* Remove all reference between the Lua stack and the coroutine stream. */
        appctx = objt_appctx(socket->s->si[0].end);
-       stream_shutdown(socket->s, SF_ERR_KILLED);
        socket->s = NULL;
        appctx->ctx.hlua_cosocket.socket = NULL;
+       appctx->ctx.hlua_cosocket.die = 1;
+       appctx_wakeup(appctx);
 
        return 0;
 }
@@ -1650,10 +1660,11 @@ __LJMP static int hlua_socket_close(lua_State *L)
                return 0;
 
        /* Close the stream and remove the associated stop task. */
-       stream_shutdown(socket->s, SF_ERR_KILLED);
        appctx = objt_appctx(socket->s->si[0].end);
        appctx->ctx.hlua_cosocket.socket = NULL;
        socket->s = NULL;
+       appctx->ctx.hlua_cosocket.die = 1;
+       appctx_wakeup(appctx);
 
        return 0;
 }
@@ -2325,6 +2336,7 @@ __LJMP static int hlua_socket_new(lua_State *L)
 
        appctx->ctx.hlua_cosocket.socket = socket;
        appctx->ctx.hlua_cosocket.connected = 0;
+       appctx->ctx.hlua_cosocket.die = 0;
        LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
        LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);