]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: applet: Add function to release appctx on error during init stage
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 12 May 2022 13:18:48 +0000 (15:18 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 17 May 2022 14:13:21 +0000 (16:13 +0200)
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
src/applet.c

index b9c581d369c384c42be54757162fb4ca87c90c5e..f8d180c7d6e8de28992ef3e5a5aff75cbe59604f 100644 (file)
@@ -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.
index 4bb345bcbffa4ca9ab8c778644a1b04e93565e93..e8b56c144f9c8353c8d721b1c8a1276b18596e15 100644 (file)
@@ -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 <size> bytes in the <appctx>, 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