Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
squash! Kill also children of the process to be killed via Ctrl+C
Try to kill Win32 processes gently upon Ctrl+C When a process is terminated via TerminateProcess(), it has no chance to do anything in the way of cleaning up. This is particularly noticeable when a lengthy Git for Windows process tries to update Git's index file and leaves behind an index.lock file. Git's idea is to remove the stale index.lock file in that case, using the signal and atexit handlers available in Linux. But those signal handlers never run. Note: this is not an 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()`. Let's use a gentler method, described in the Dr Dobb's article "A Safer Alternative to TerminateProcess()" by Andrew Tucker (July 1, 1999), http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547 Essentially, we inject a new thread into the running process that does nothing else than running the ExitProcess() function. If that fails, or when the process still lives on after 10 seconds, we fall back to calling TerminateProcess(), but in that case we take pains to kill also processes directly or indirectly spawned by that process; TerminateProcess() does not give any process a chance to terminate its child processes. Originally, we wanted to call the function `GenerateConsoleCtrlEvent()` for SIGTERM with console processes, but 1) that may not work as it requires a process *group* ID (i.e. the process ID of the initial process group member, which we may not have), and 2) it does not kill the process *tree* on Windows 7 and earlier. Note: 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. While at it, we also fix the exit status: the convention is to add 128 to the signal number, to give exit code handlers a chance to detect an "exit by signal" condition. This fixes the MSYS2 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). Note that this special-handling is only necessary when *MSYS2* 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 <johannes.schindelin@gmx.de>
- Loading branch information