From: Christopher Faulet Date: Thu, 19 Nov 2026 18:10:00 +0000 (+0100) Subject: BUG/MEDIUM: applets: do not wake up applets before the end of the request analysis X-Git-Url: https://git.kaiwu.me/http/control.html?a=commitdiff_plain;h=8236299dee6a32f22fb825e749692b6588b50a38;p=haproxy.git BUG/MEDIUM: applets: do not wake up applets before the end of the request analysis The applet is woken up as soon as it is created by sc_applet_create(). But the request analysis is not over at this point and it may still be paused, typically by a "http-request wait-for-body" rule, while the applet is already attached to the back stream connector (the cache after a cache-use hit, for instance). Thus, the applet could start to consume the request and to produce its response before the end of the request analysis, and the response analysers were evaluated in the same time. It is unexpected and it could lead to weird issues. To fix the issue, the way the applets are created is changed. sc_applet_create() no longer schedules the applet and it no longer switches the stream connector to the ready state. The applet remains in SC_ST_INI state, in a dormant state, and the request analysis can continue, undisturbed. The applet is woken up by back_handle_st_req(), when the request analysis is over and the stream connector switches from SC_ST_INI to SC_ST_REQ and then to SC_ST_RDY. Thus, process_use_service() was reviewed accordingly. Because the use-service action is terminal and it never yields, there is no re-entry in this function, so the re-entry branch was removed. And the applet is not woken up by the action anymore. It will be done by back_handle_st_req(). Note that a re-entry would now fail in sc_applet_create(), because the SE_FL_DETACHED flag is cleared once an applet is attached. Note this design change reverts, in intent, the commit a33ff7a8a ("BUG/MEDIUM: conn-stream: Set back CS to RDY state when the appctx is created"). Here, the stream connector is set to the ready state when the applet is woken up, not when the appctx is created, so the ambiguity discussed in this commit remains solved: there is no target installed on a stream connector in SC_ST_INI state, and the stream connector is switched to SC_ST_RDY state before the applet starts to work. This patch could be backported to all supported versions. But because it is a design change, it is probably better to not backport it lower than the 3.2, and only after a period of observation. It relies on "BUG/MEDIUM: stconn: Do not install an applet on a busy stream connector" --- diff --git a/src/backend.c b/src/backend.c index 62bc76e22..d5ab12fbe 100644 --- a/src/backend.c +++ b/src/backend.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -2667,33 +2668,37 @@ void back_handle_st_req(struct stream *s) DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_CS_ST, s); if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) { - struct appctx *appctx; - - /* The target is an applet but the SC is in SC_ST_REQ. Thus it - * means no appctx are attached to the SC. Otherwise, it will be - * in SC_ST_RDY state. So, try to create the appctx now. - */ - BUG_ON(sc_appctx(sc)); - appctx = sc_applet_create(sc, objt_applet(s->target)); - if (!appctx) { - /* No more memory, let's immediately abort. Force the - * error code to ignore the ERR_LOCAL which is not a - * real error. - */ - s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK); - - sc_abort(sc); - sc_shutdown(sc); - sc->flags |= SC_FL_ERROR; - s->conn_err_type = STRM_ET_CONN_RES; - sc->state = SC_ST_CLO; - if (s->srv_error) - s->srv_error(s, sc); - DBG_TRACE_STATE("failed to register applet", STRM_EV_STRM_PROC|STRM_EV_CS_ST|STRM_EV_STRM_ERR, s); - goto end; + struct appctx *appctx = sc_appctx(sc); + + if (appctx == NULL) { + appctx = sc_applet_create(sc, objt_applet(s->target)); + if (!appctx) { + /* No more memory, let's immediately abort. Force the + * error code to ignore the ERR_LOCAL which is not a + * real error. + */ + s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK); + + sc_abort(sc); + sc_shutdown(sc); + sc->flags |= SC_FL_ERROR; + s->conn_err_type = STRM_ET_CONN_RES; + sc->state = SC_ST_CLO; + if (s->srv_error) + s->srv_error(s, sc); + DBG_TRACE_STATE("failed to register applet", STRM_EV_STRM_PROC|STRM_EV_CS_ST|STRM_EV_STRM_ERR, s); + goto end; + } + DBG_TRACE_STATE("applet registered", STRM_EV_STRM_PROC|STRM_EV_CS_ST, s); } - DBG_TRACE_STATE("applet registered", STRM_EV_STRM_PROC|STRM_EV_CS_ST, s); + BUG_ON(appctx->applet != __objt_applet(s->target)); + + sc->state = SC_ST_RDY; + sc_will_read(sc); + applet_need_more_data(appctx); + appctx_wakeup(appctx); + DBG_TRACE_STATE("applet ready", STRM_EV_STRM_PROC|STRM_EV_CS_ST, s); goto end; } diff --git a/src/stconn.c b/src/stconn.c index b2c096c74..327218edd 100644 --- a/src/stconn.c +++ b/src/stconn.c @@ -551,10 +551,8 @@ struct appctx *sc_applet_create(struct stconn *sc, struct applet *app) return NULL; } appctx->t->nice = __sc_strm(sc)->task->nice; - applet_need_more_data(appctx); - appctx_wakeup(appctx); - - sc->state = SC_ST_RDY; + applet_wont_consume(appctx); + sc_wont_read(sc); return appctx; } diff --git a/src/stream.c b/src/stream.c index 38edf971f..bf9fd4486 100644 --- a/src/stream.c +++ b/src/stream.c @@ -1170,12 +1170,20 @@ void sess_set_term_flags(struct stream *s) } } -/* This function parses the use-service action ruleset. It executes +/* This function executes the use-service action ruleset. It executes * the associated ACL and set an applet as a stream or txn final node. - * it returns ACT_RET_ERR if an error occurs, the proxy left in - * consistent state. It returns ACT_RET_STOP in success case because - * use-service must be a terminal action. Returns ACT_RET_YIELD - * if the initialisation function require more data. + * It returns ACT_RET_ERR if an error occurs, the proxy left in + * consistent state, or ACT_RET_STOP in success case because use-service + * must be a terminal action. + * + * The applet is not woken up here. sc_applet_create() attaches it to the + * back stream connector in a dormant state: it remains in SC_ST_INI + * state and it is not scheduled. This way, the request analysis is not + * disturbed by the applet: it will not try to consume the request + * before the analysis ends and it cannot produce a premature response. + * The applet is woken up by back_handle_st_req(), when the request + * analysis is over and the stream connector switches to the ready + * state. */ enum act_return process_use_service(struct act_rule *rule, struct proxy *px, struct session *sess, struct stream *s, int flags) @@ -1183,20 +1191,18 @@ enum act_return process_use_service(struct act_rule *rule, struct proxy *px, { struct appctx *appctx; - /* Initialises the applet if it is required. */ - if (flags & ACT_OPT_FIRST) { - /* Register applet. this function schedules the applet. */ - stream_set_target(s, &rule->applet.obj_type); - appctx = sc_applet_create(s->scb, objt_applet(s->target)); - if (unlikely(!appctx)) - return ACT_RET_ERR; + /* Register the applet. It is attached to the back stream connector, + * in a dormant state, and it will be woken up when the request + * analysis is over. + */ + stream_set_target(s, &rule->applet.obj_type); + appctx = sc_applet_create(s->scb, objt_applet(s->target)); + if (unlikely(!appctx)) + return ACT_RET_ERR; - /* Finish initialisation of the context. */ - if (appctx_init(appctx) == -1) - return ACT_RET_ERR; - } - else - appctx = __sc_appctx(s->scb); + /* Finish initialisation of the context. */ + if (appctx_init(appctx) == -1) + return ACT_RET_ERR; if (rule->from != ACT_F_HTTP_REQ) { /* report it if the request was intercepted by the frontend */ @@ -1207,9 +1213,6 @@ enum act_return process_use_service(struct act_rule *rule, struct proxy *px, s->flags |= SF_ASSIGNED; } - /* Now we can schedule the applet. */ - applet_need_more_data(appctx); - appctx_wakeup(appctx); return ACT_RET_STOP; }