Skip to content

Commit 3687e63

Browse files
committed
Kill also children of the process to be killed via Ctrl+C
When a Win32 process needs to be terminated, the child processes it spawned off also need to be terminated. This is no issue for MSys2 processes because MSys2 emulates Unix' signal system accurately, both for the process sending the kill signal and the process receiving it. Win32 processes do not have such a signal handler, though, instead MSys2 shuts them down via `TerminateProcess()`. As `TerminateProcess()` leaves the Win32 process no chance to do anything, and also does not care about child processes, we have to grow a different solution. For console processes, it should be enough to call `GenerateConsoleCtrlEvent()`, but sadly even then this seems to handle child processes correctly only on Windows 8 but not Windows 7. So let's identify the tree of processes spawned directly and indirectly from the process to be killed, and kill them all. To do so, we do not use the NtQueryInformationProcess() function because 1) it is marked internal and subject to change at any time of Microsoft's choosing, and 2) it does not even officially report the child/parent relationship (the pid of the parent process is stored in the `Reserved3` slot of the `PROCESS_BASIC_INFORMATION` structure). Instead, we resort to the Toolhelp32 API -- which happily also works on 64-bit Windows -- to enumerate the process tree and reconstruct the process tree rooted in the process we intend to kill. This fixes 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). Note that this special-handling is only necessary when *MSys* handles the Ctrl+C event, e.g. when interrupting a process started from within mintty or any other non-cmd-based terminal emulator. If the process was started from within `cmd.exe`'s terminal window, child processes are already killed appropriately upon Ctrl+C. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6c92a75 commit 3687e63

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

winsup/cygwin/exceptions.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,10 @@ sigpacket::process ()
15471547
if (have_execed)
15481548
{
15491549
sigproc_printf ("terminating captive process");
1550-
TerminateProcess (ch_spawn, sigExeced = si.si_signo);
1550+
if ((sigExeced = si.si_signo) == SIGINT)
1551+
kill_process_tree (GetProcessId (ch_spawn), sigExeced = si.si_signo);
1552+
else
1553+
TerminateProcess (ch_spawn, sigExeced = si.si_signo);
15511554
}
15521555
/* Dispatch to the appropriate function. */
15531556
sigproc_printf ("signal %d, signal handler %p", si.si_signo, handler);

winsup/cygwin/include/cygwin/signal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ extern const char *sys_siglist[];
414414
extern const char __declspec(dllimport) *sys_sigabbrev[];
415415
extern const char __declspec(dllimport) *sys_siglist[];
416416
#endif
417+
void kill_process_tree(pid_t pid, int sig);
417418

418419
#ifdef __cplusplus
419420
}

winsup/cygwin/signal.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Cygwin license. Please consult the file "CYGWIN_LICENSE" for
1010
details. */
1111

1212
#include "winsup.h"
13+
#include <tlhelp32.h>
1314
#include <stdlib.h>
1415
#include <sys/cygwin.h>
1516
#include "pinfo.h"
@@ -358,6 +359,62 @@ killpg (pid_t pgrp, int sig)
358359
return kill (-pgrp, sig);
359360
}
360361

362+
/**
363+
* Terminates the process corresponding to the process ID and all of its
364+
* directly and indirectly spawned subprocesses.
365+
*/
366+
extern "C" void
367+
kill_process_tree(pid_t pid, int sig)
368+
{
369+
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
370+
PROCESSENTRY32 entry;
371+
DWORD pids[16384];
372+
int max_len = sizeof(pids) / sizeof(*pids), i, len;
373+
374+
pids[0] = (DWORD) pid;
375+
len = 1;
376+
377+
/*
378+
* Even if Process32First()/Process32Next() seem to traverse the
379+
* processes in topological order (i.e. parent processes before
380+
* child processes), there is nothing in the Win32 API documentation
381+
* suggesting that this is guaranteed.
382+
*
383+
* Therefore, run through them at least twice and stop when no more
384+
* process IDs were added to the list.
385+
*/
386+
for (;;) {
387+
int orig_len = len;
388+
389+
memset(&entry, 0, sizeof(entry));
390+
entry.dwSize = sizeof(entry);
391+
392+
if (!Process32First(snapshot, &entry))
393+
break;
394+
395+
do {
396+
for (i = len - 1; i >= 0; i--) {
397+
if (pids[i] == entry.th32ProcessID)
398+
break;
399+
if (pids[i] == entry.th32ParentProcessID)
400+
pids[len++] = entry.th32ProcessID;
401+
}
402+
} while (len < max_len && Process32Next(snapshot, &entry));
403+
404+
if (orig_len == len || len >= max_len)
405+
break;
406+
}
407+
408+
for (i = len - 1; i >= 0; i--) {
409+
HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, pids[i]);
410+
411+
if (process) {
412+
TerminateProcess(process, sig << 8);
413+
CloseHandle(process);
414+
}
415+
}
416+
}
417+
361418
extern "C" void
362419
abort (void)
363420
{

0 commit comments

Comments
 (0)