]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: mworker: fix a FD leak of a sockpair upon a failed reload
authorWilliam Lallemand <wlallemand@haproxy.org>
Fri, 28 Jan 2022 20:17:30 +0000 (21:17 +0100)
committerWilliam Lallemand <wlallemand@haproxy.org>
Fri, 28 Jan 2022 22:47:43 +0000 (23:47 +0100)
When starting HAProxy in master-worker, the master pre-allocate a struct
mworker_proc and do a socketpair() before the configuration parsing. If
the configuration loading failed, the FD are never closed because they
aren't part of listener, they are not even in the fdtab.

This patch fixes the issue by cleaning the mworker_proc structure that
were not asssigned a process, and closing its FDs.

Must be backported as far as 2.0, the srv_drop() only frees the memory
and could be dropped since it's done before an exec().

include/haproxy/mworker.h
src/haproxy.c
src/mworker.c

index 0e166ac090dca181b97d210b06ee9898cf812e67..09d6d587e25edcf4ca86cfbc1dc782877087f181 100644 (file)
@@ -42,5 +42,6 @@ int mworker_ext_launch_all(void);
 void mworker_kill_max_reloads(int sig);
 
 void mworker_free_child(struct mworker_proc *);
+void mworker_cleanup_proc();
 
 #endif /* _HAPROXY_MWORKER_H_ */
index 6013875359b9088b5a18b9321734ad6a65be432e..70ce708f3eba3940b2756b933ce5c963810a8998 100644 (file)
@@ -673,6 +673,7 @@ static void mworker_reexec()
 #endif
        setenv("HAPROXY_MWORKER_REEXEC", "1", 1);
 
+       mworker_cleanup_proc();
        mworker_proc_list_to_env(); /* put the children description in the env */
 
        /* ensure that we close correctly every listeners before reexecuting */
index 8b0b06ba029f7a29167d5afb2ecc72e15f8e8c42..fa3c1a25d2224813d4c5b9897fd113f1b99d6c40 100644 (file)
@@ -457,6 +457,36 @@ void mworker_cleanlisteners()
        }
 }
 
+/* Upon a configuration loading error some mworker_proc and FDs/server were
+ * assigned but the worker was never forked, we must close the FDs and
+ * remove the server
+ */
+void mworker_cleanup_proc()
+{
+       struct mworker_proc *child, *it;
+
+       list_for_each_entry_safe(child, it, &proc_list, list) {
+
+               if (child->pid == -1) {
+                       /* Close the socketpair master side.  We don't need to
+                        * close the worker side, because it's stored in the
+                        * GLOBAL cli listener which was supposed to be in the
+                        * worker and which will be closed in
+                        * mworker_cleanlisteners()
+                        */
+                       if (child->ipc_fd[0] > -1)
+                               close(child->ipc_fd[0]);
+                       if (child->srv) {
+                               /* only exists if we created a master CLI listener */
+                               srv_drop(child->srv);
+                       }
+                       LIST_DELETE(&child->list);
+                       mworker_free_child(child);
+               }
+       }
+}
+
+
 /*  Displays workers and processes  */
 static int cli_io_handler_show_proc(struct appctx *appctx)
 {