Skip to content

Commit

Permalink
fsmonitor--daemon: background daemon must free the console on windows
Browse files Browse the repository at this point in the history
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 <jeffhost@microsoft.com>
  • Loading branch information
jeffhostetler authored and dscho committed Aug 16, 2021
1 parent 7b37f49 commit e3fc6ef
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions builtin/fsmonitor--daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 <n> ipc worker threads")),
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e3fc6ef

Please sign in to comment.