Skip to content

Commit 8f63aba

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 62f68aa commit 8f63aba

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

compat/mingw.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,10 +1432,44 @@ struct pinfo_t {
14321432
static struct pinfo_t *pinfo = NULL;
14331433
CRITICAL_SECTION pinfo_cs;
14341434

1435+
#ifndef SIGRTMAX
1436+
#define SIGRTMAX 63
1437+
#endif
1438+
1439+
static void kill_child_processes_on_signal(void)
1440+
{
1441+
DWORD status;
1442+
1443+
/*
1444+
* Only continue if the process was terminated by a signal, as
1445+
* indicated by the exit status (128 + sig_no).
1446+
*
1447+
* As we are running in an atexit() handler, the exit code has been
1448+
* set at this stage by the ExitProcess() function already.
1449+
*/
1450+
if (!GetExitCodeProcess(GetCurrentProcess(), &status) ||
1451+
status <= 128 || status > 128 + SIGRTMAX)
1452+
return;
1453+
1454+
EnterCriticalSection(&pinfo_cs);
1455+
1456+
while (pinfo) {
1457+
struct pinfo_t *info = pinfo;
1458+
pinfo = pinfo->next;
1459+
if (exit_process(info->proc, status))
1460+
/* the handle is still valid in case of error */
1461+
CloseHandle(info->proc);
1462+
free(info);
1463+
}
1464+
1465+
LeaveCriticalSection(&pinfo_cs);
1466+
}
1467+
14351468
static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenv,
14361469
const char *dir,
14371470
int prepend_cmd, int fhin, int fhout, int fherr)
14381471
{
1472+
static int atexit_handler_initialized;
14391473
STARTUPINFOW si;
14401474
PROCESS_INFORMATION pi;
14411475
struct strbuf args;
@@ -1445,6 +1479,17 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14451479
HANDLE cons;
14461480
const char *strace_env;
14471481

1482+
if (!atexit_handler_initialized) {
1483+
atexit_handler_initialized = 1;
1484+
/*
1485+
* On Windows, there is no POSIX signaling. Instead, we inject
1486+
* a thread calling ExitProcess(128 + sig_no); and that calls
1487+
* the *atexit* handlers. Catch this condition and kill child
1488+
* processes with the same signal.
1489+
*/
1490+
atexit(kill_child_processes_on_signal);
1491+
}
1492+
14481493
do_unset_environment_variables();
14491494

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

0 commit comments

Comments
 (0)