From e3fc6efce3848c00fc2771f12b0800d8f442507b Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Tue, 3 Aug 2021 15:31:45 -0400 Subject: [PATCH] fsmonitor--daemon: background daemon must free the console on windows Teach "git fsmonitor--daemon run" to call FreeConsole() when started in the background by "git fsmonitor--daemon start" on Windows. The background process was holding a handle to the inherited Win32 console despite being passed stdin/out/err set to /dev/null. This caused command prompts and powershell terminal windows to hang in "exit" waiting for the last console handle to be released. (This problem was not seen in git-bash type terminal windows because they don't have a Win32 console attached to them.) Signed-off-by: Jeff Hostetler --- builtin/fsmonitor--daemon.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c index a4e3f6e3099ff3..d9a9b8e6b832ad 100644 --- a/builtin/fsmonitor--daemon.c +++ b/builtin/fsmonitor--daemon.c @@ -1302,7 +1302,7 @@ static int fsmonitor_run_daemon(void) return err; } -static int try_to_run_foreground_daemon(void) +static int try_to_run_foreground_daemon(int free_console) { /* * Technically, we don't need to probe for an existing daemon @@ -1320,6 +1320,11 @@ static int try_to_run_foreground_daemon(void) the_repository->worktree); fflush(stdout); +#ifdef GIT_WINDOWS_NATIVE + if (free_console) + FreeConsole(); +#endif + return !!fsmonitor_run_daemon(); } @@ -1351,6 +1356,7 @@ static int spawn_fsmonitor(pid_t *pid) strvec_push(&args, git_exe); strvec_push(&args, "fsmonitor--daemon"); strvec_push(&args, "run"); + strvec_push(&args, "--free-console"); strvec_pushf(&args, "--ipc-threads=%d", fsmonitor__ipc_threads); *pid = mingw_spawnvpe(args.v[0], args.v, NULL, NULL, in, out, out); @@ -1514,8 +1520,10 @@ static int try_to_start_background_daemon(void) int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix) { const char *subcmd; + int free_console = 0; struct option options[] = { + OPT_BOOL(0, "free-console", &free_console, N_("free console")), OPT_INTEGER(0, "ipc-threads", &fsmonitor__ipc_threads, N_("use ipc worker threads")), @@ -1559,7 +1567,7 @@ int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix) return !!try_to_start_background_daemon(); if (!strcmp(subcmd, "run")) - return !!try_to_run_foreground_daemon(); + return !!try_to_run_foreground_daemon(free_console); if (!strcmp(subcmd, "stop")) return !!do_as_client__send_stop();