]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: hlua: Initialize appctx used by a lua socket on connect only
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 27 Sep 2023 15:47:45 +0000 (17:47 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 4 Oct 2023 13:34:13 +0000 (15:34 +0200)
Ths appctx used by a lua socket was synchronously initialized after the
appctx creation. The connect itself is performed later. However it is an
issue because the script may be interrupted beteween the two operation. In
this case, the stream attached to the appctx is woken up before any
destination is set. The stream will try to connect but without destination,
it fails. When the lua script is rescheduled and the connect is performed,
the connection has already failed and an error is returned.

To fix the issue, we must be sure to not woken up the stream before the
connect. To do so, we must defer the appctx initilization. It is now perform
on connect.

This patch relies on the following commits:

  * MINOR: hlua: Test the hlua struct first when the lua socket is connecting
  * MINOR: hlua: Save the lua socket's server in its context
  * MINOR: hlua: Save the lua socket's timeout in its context
  * MINOR: hlua: Don't preform operations on a not connected socket
  * MINOR: hlua: Set context's appctx when the lua socket is created

All the series must be backported as far as 2.6.

src/hlua.c

index fbed29946e876f069674c20a8b27ab7ab4cfd2ce..c686f222a848d40bfbf619fbcea9bf7f11de6270 100644 (file)
@@ -2345,7 +2345,7 @@ static int hlua_socket_init(struct appctx *appctx)
 
        /* Force destination server. */
        s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
-       s->target = &socket_tcp->obj_type;
+       s->target = &csk_ctx->srv->obj_type;
 
        if (csk_ctx->timeout) {
                s->sess->fe->timeout.connect = csk_ctx->timeout;
@@ -3146,7 +3146,6 @@ __LJMP static int hlua_socket_connect(struct lua_State *L)
        struct sockaddr_storage *addr;
        struct xref *peer;
        struct stconn *sc;
-       struct stream *s;
 
        /* Get hlua struct, or NULL if we execute from main lua state */
        hlua = hlua_gethlua(L);
@@ -3217,10 +3216,18 @@ __LJMP static int hlua_socket_connect(struct lua_State *L)
                }
        }
 
-       csk_ctx = container_of(peer, struct hlua_csk_ctx, xref);
        appctx = csk_ctx->appctx;
+       if (appctx_sc(appctx)) {
+               xref_unlock(&socket->xref, peer);
+               WILL_LJMP(luaL_error(L, "connect: connect already performed\n"));
+       }
+
+       if (appctx_init(appctx) == -1) {
+               xref_unlock(&socket->xref, peer);
+               WILL_LJMP(luaL_error(L, "connect: fail to init applet."));
+       }
+
        sc = appctx_sc(appctx);
-       s = __sc_strm(sc);
 
        if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) {
                xref_unlock(&socket->xref, peer);
@@ -3242,9 +3249,7 @@ __LJMP static int hlua_socket_connect(struct lua_State *L)
        }
        xref_unlock(&socket->xref, peer);
 
-       task_wakeup(s->task, TASK_WOKEN_INIT);
        /* Return yield waiting for connection. */
-
        MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
 
        return 0;
@@ -3255,7 +3260,6 @@ __LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
 {
        struct hlua_socket *socket;
        struct xref *peer;
-       struct stream *s;
 
        MAY_LJMP(check_args(L, 3, "connect_ssl"));
        socket  = MAY_LJMP(hlua_checksocket(L, 1));
@@ -3267,10 +3271,8 @@ __LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
                return 1;
        }
 
-       s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx);
        container_of(peer, struct hlua_csk_ctx, xref)->srv = socket_ssl;
 
-       s->target = &socket_ssl->obj_type;
        xref_unlock(&socket->xref, peer);
        return MAY_LJMP(hlua_socket_connect(L));
 }
@@ -3390,11 +3392,6 @@ __LJMP static int hlua_socket_new(lua_State *L)
        LIST_INIT(&ctx->wake_on_write);
        LIST_INIT(&ctx->wake_on_read);
 
-       if (appctx_init(appctx) == -1) {
-               hlua_pusherror(L, "socket: fail to init applet.");
-               goto out_fail_appctx;
-       }
-
        /* Initialise cross reference between stream and Lua socket object. */
        xref_create(&socket->xref, &ctx->xref);
        return 1;