]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: stats: use refcount to protect dynamic server on dump
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 25 Aug 2021 12:39:53 +0000 (14:39 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 25 Aug 2021 13:53:43 +0000 (15:53 +0200)
A dynamic server may be deleted at runtime at the same moment when the
stats applet is pointing to it. Use the server refcount to prevent
deletion in this case.

This should be backported up to 2.4, with an observability period of 2
weeks. Note that it requires the dynamic server refcounting feature
which has been implemented on 2.5; the following commits are required :

- MINOR: server: implement a refcount for dynamic servers
- BUG/MINOR: server: do not use refcount in free_server in stopping mode
- MINOR: server: return the next srv instance on free_server

include/haproxy/server.h
src/server.c
src/stats.c

index 767764880858a3599f9c1beaeeac1df5a05dbd28..b39be2a1ce4aa7cd24b563a59b91434cd62b6ad9 100644 (file)
@@ -59,6 +59,7 @@ int srv_set_addr_via_libc(struct server *srv, int *err_code);
 int srv_init_addr(void);
 struct server *cli_find_server(struct appctx *appctx, char *arg);
 struct server *new_server(struct proxy *proxy);
+void srv_use_dynsrv(struct server *srv);
 struct server *free_server(struct server *srv);
 int srv_init_per_thr(struct server *srv);
 
index 583cce7f5b61e54799c583ba0696055ee04ccbb2..54a569f7234052606dffffe984f49bf91af8fb25 100644 (file)
@@ -2197,7 +2197,7 @@ struct server *new_server(struct proxy *proxy)
 }
 
 /* Increment the dynamic server refcount. */
-static void srv_use_dynsrv(struct server *srv)
+void srv_use_dynsrv(struct server *srv)
 {
        BUG_ON(!(srv->flags & SRV_F_DYNAMIC));
        HA_ATOMIC_INC(&srv->refcount_dynsrv);
index b767ce5d4673ee5ba1e710a750850c2f6e5b094c..8140d87f953a4402d9732eebc36015c6b4b201ad 100644 (file)
@@ -3111,8 +3111,20 @@ int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
                /* fall through */
 
        case STAT_PX_ST_SV:
-               /* obj2 points to servers list as initialized above */
-               for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
+               /* obj2 points to servers list as initialized above.
+                *
+                * A dynamic server may be removed during the stats dumping.
+                * Temporarily increment its refcount to prevent its
+                * anticipated cleaning. Call free_server to release it.
+                */
+               for (; appctx->ctx.stats.obj2 != NULL;
+                      appctx->ctx.stats.obj2 =
+                       (!(sv->flags & SRV_F_DYNAMIC)) ? sv->next : free_server(sv)) {
+
+                       sv = appctx->ctx.stats.obj2;
+                       if (sv->flags & SRV_F_DYNAMIC)
+                               srv_use_dynsrv(sv);
+
                        if (htx) {
                                if (htx_almost_full(htx))
                                        goto full;
@@ -3122,11 +3134,12 @@ int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
                                        goto full;
                        }
 
-                       sv = appctx->ctx.stats.obj2;
-
                        if (appctx->ctx.stats.flags & STAT_BOUND) {
-                               if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
+                               if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV))) {
+                                       if (sv->flags & SRV_F_DYNAMIC)
+                                               free_server(appctx->ctx.stats.obj2);
                                        break;
+                               }
 
                                if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
                                        continue;