From d0c4ec04b874f116fd82f445e7158c69ac1d6289 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Thu, 12 May 2022 15:18:48 +0200 Subject: [PATCH] MINOR: applet: Add function to release appctx on error during init stage appctx_free_on_early_error() must be used to release a freshly created frontend appctx if an error occurred during the init stage. It takes care to release the stream instead of the appctx if it exists. For a backend appctx, it just calls appctx_free(). --- include/haproxy/applet.h | 1 + src/applet.c | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/haproxy/applet.h b/include/haproxy/applet.h index b9c581d36..f8d180c7d 100644 --- a/include/haproxy/applet.h +++ b/include/haproxy/applet.h @@ -42,6 +42,7 @@ void appctx_shut(struct appctx *appctx); struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp); int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input); +void appctx_free_on_early_error(struct appctx *appctx); /* Helper function to call .init applet callback function, if it exists. Returns 0 * on success and -1 on error. diff --git a/src/applet.c b/src/applet.c index 4bb345bcb..e8b56c144 100644 --- a/src/applet.c +++ b/src/applet.c @@ -81,7 +81,8 @@ struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp) * It returns 0 on success and -1 on error. In this case, it is the caller * responsibility to release the appctx. However, the session is released if it * was created. On success, if an error is encountered in the caller function, - * the stream must be released instead of the appctx. + * the stream must be released instead of the appctx. To be sure, + * appctx_free_on_early_error() must be called in this case. */ int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input) { @@ -100,6 +101,21 @@ int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buff return 0; } +/* Release function to call when an error occurred during init stage of a + * frontend appctx. For a backend appctx, it just calls appctx_free() + */ +void appctx_free_on_early_error(struct appctx *appctx) +{ + /* If a frontend apctx is attached to a conn-stream, release the stream + * instead of the appctx. + */ + if (!(appctx->endp->flags & CS_EP_ORPHAN) && !(appctx_cs(appctx)->flags & CS_FL_ISBACK)) { + stream_free(appctx_strm(appctx)); + return; + } + appctx_free(appctx); +} + /* reserves a command context of at least bytes in the , for * use by a CLI command or any regular applet. The pointer to this context is * stored in ctx.svcctx and is returned. The caller doesn't need to release