]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: server: adjust initialization order for dynamic servers
authorWilly Tarreau <w@1wt.eu>
Tue, 24 Feb 2026 19:28:50 +0000 (20:28 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 26 Feb 2026 07:24:03 +0000 (08:24 +0100)
It appears that in cli_parse_add_server(), we're calling srv_alloc_lb()
and stats_allocate_proxy_counters_internal() before srv_preinit() which
allocates the thread groups. LB algos can make use of the per_tgrp part
which is initialized by srv_preinit(). Fortunately for now no algo uses
both tgrp and ->server_init() so this explains why this remained
unnoticed to date. Also, extra counters will soon require per_tgrp to
already be initialized. So let's move these between srv_preinit() and
srv_postinit(). It's possible that other parts will have to be moved
in between.

This could be backported to recent versions for the sake of safety but
it looks like the current code cannot tell the difference.

src/server.c

index 66e1513d3ccdce883ef881c0fb92718e348aa1f2..10de4faa325091c528ea07c9ae13a60803dec27a 100644 (file)
@@ -6227,18 +6227,6 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
                }
        }
 
-       if (!srv_alloc_lb(srv, be)) {
-               ha_alert("Failed to initialize load-balancing data.\n");
-               goto out;
-       }
-
-       if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
-                                                   COUNTERS_SV,
-                                                   STATS_PX_CAP_SRV)) {
-               ha_alert("failed to allocate extra counters for server.\n");
-               goto out;
-       }
-
        /* ensure minconn/maxconn consistency */
        srv_minmax_conn_apply(srv);
 
@@ -6289,6 +6277,19 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
        errcode = srv_preinit(srv);
        if (errcode)
                goto out;
+
+       if (!srv_alloc_lb(srv, be)) {
+               ha_alert("Failed to initialize load-balancing data.\n");
+               goto out;
+       }
+
+       if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
+                                                   COUNTERS_SV,
+                                                   STATS_PX_CAP_SRV)) {
+               ha_alert("failed to allocate extra counters for server.\n");
+               goto out;
+       }
+
        errcode = srv_postinit(srv);
        if (errcode)
                goto out;