Skip to content

Commit 9cbe062

Browse files
committed
mingw: kill unterminated child processes on signals
Git for Windows' MSYS2 runtime was just adjusted to kill processes gently, by injecting a thread that calls ExitProcess(). In case of signals (such as when handling Ctrl+C in a MinTTY window), the exit code is 128 + sign_no, as expected by Git's source code. However, as there is no POSIX signal handling on Windows, no signal handlers are called. Instead, functions registered via atexit() are called. We work around that by testing the exit code explicitly. This fixes the Git for Windows side of the bug where interrupting `git clone https://...` would send the spawned-off `git remote-https` process into the background instead of interrupting it, i.e. the clone would continue and its progress would be reported mercilessly to the console window without the user being able to do anything about it (short of firing up the task manager and killing the appropriate task manually). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 97dcca4 commit 9cbe062

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

compat/mingw.c

+45
Original file line numberDiff line numberDiff line change
@@ -1461,10 +1461,44 @@ struct pinfo_t {
14611461
static struct pinfo_t *pinfo = NULL;
14621462
CRITICAL_SECTION pinfo_cs;
14631463

1464+
#ifndef SIGRTMAX
1465+
#define SIGRTMAX 63
1466+
#endif
1467+
1468+
static void kill_child_processes_on_signal(void)
1469+
{
1470+
DWORD status;
1471+
1472+
/*
1473+
* Only continue if the process was terminated by a signal, as
1474+
* indicated by the exit status (128 + sig_no).
1475+
*
1476+
* As we are running in an atexit() handler, the exit code has been
1477+
* set at this stage by the ExitProcess() function already.
1478+
*/
1479+
if (!GetExitCodeProcess(GetCurrentProcess(), &status) ||
1480+
status <= 128 || status > 128 + SIGRTMAX)
1481+
return;
1482+
1483+
EnterCriticalSection(&pinfo_cs);
1484+
1485+
while (pinfo) {
1486+
struct pinfo_t *info = pinfo;
1487+
pinfo = pinfo->next;
1488+
if (exit_process(info->proc, status))
1489+
/* the handle is still valid in case of error */
1490+
CloseHandle(info->proc);
1491+
free(info);
1492+
}
1493+
1494+
LeaveCriticalSection(&pinfo_cs);
1495+
}
1496+
14641497
static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenv,
14651498
const char *dir,
14661499
int prepend_cmd, int fhin, int fhout, int fherr)
14671500
{
1501+
static int atexit_handler_initialized;
14681502
STARTUPINFOW si;
14691503
PROCESS_INFORMATION pi;
14701504
struct strbuf args;
@@ -1474,6 +1508,17 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14741508
HANDLE cons;
14751509
const char *strace_env;
14761510

1511+
if (!atexit_handler_initialized) {
1512+
atexit_handler_initialized = 1;
1513+
/*
1514+
* On Windows, there is no POSIX signaling. Instead, we inject
1515+
* a thread calling ExitProcess(128 + sig_no); and that calls
1516+
* the *atexit* handlers. Catch this condition and kill child
1517+
* processes with the same signal.
1518+
*/
1519+
atexit(kill_child_processes_on_signal);
1520+
}
1521+
14771522
do_unset_environment_variables();
14781523

14791524
/* Determine whether or not we are associated to a console */

0 commit comments

Comments
 (0)