transport-helper, connect: use clean_on_exit to reap children on abnormal exit

When a long-running service (e.g., a source indexer) runs as PID 1 inside a container and repeatedly spawns git, git may in turn spawn child processes such as git-remote-https or ssh. If git exits abnormally (e.g., via exit(128) on a transport error), the normal cleanup paths (disconnect_helper, finish_connect) are bypassed, and these children are never waited on. The children are reparented to PID 1, which does not reap them, so they accumulate as zombies over time. Set clean_on_exit and wait_after_clean on child_process structs in both transport-helper.c and connect.c so that the existing run-command cleanup infrastructure handles reaping on any exit path. This avoids rolling custom atexit handlers that call finish_command(), which could deadlock if the child is blocked waiting for the parent to close a pipe. The clean_on_exit mechanism sends SIGTERM first, then waits, ensuring the child terminates promptly. It also handles signal-based exits, not just atexit. Signed-off-by: Andrew Au <cshung@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Andrew Au committed Mar 12, 2026 at 21:49 UTC dd3693eb0859274d62feac8047e1d486b3beaf31
2 files changed +6
connect.c
+4
@@ -1054,6 +1054,8 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
1054 strvec_push(&proxy->args, port);
1055 proxy->in = -1;
1056 proxy->out = -1;
1057 + proxy->clean_on_exit = 1;
1058 + proxy->wait_after_clean = 1;
1059 if (start_command(proxy))
1060 die(_("cannot start proxy %s"), git_proxy_command);
1061 fd[0] = proxy->out; /* read from proxy stdout */
@@ -1515,6 +1517,8 @@ struct child_process *git_connect(int fd[2], const char *url,
1517 }
1518 strvec_push(&conn->args, cmd.buf);
1519
1520 + conn->clean_on_exit = 1;
1521 + conn->wait_after_clean = 1;
1522 if (start_command(conn))
1523 die(_("unable to fork"));
1524
transport-helper.c
+2
@@ -154,6 +154,8 @@ static struct child_process *get_helper(struct transport *transport)
154
155 helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */
156
157 + helper->clean_on_exit = 1;
158 + helper->wait_after_clean = 1;
159 code = start_command(helper);
160 if (code < 0 && errno == ENOENT)
161 die(_("unable to find remote helper for '%s'"), data->name);