Skip to content

Commit 2f017ac

Browse files
committedDec 10, 2023
Avoid making it look like kill_process works on Windows
This changes the code in Git.execute's local kill_process function, which it uses as the timed callback for kill_after_timeout, to remove code that is unnecessary because kill_process doesn't support Windows, and to avoid giving the false impression that its code could be used unmodified on Windows without serious problems. - Raise AssertionError explicitly if it is called on Windows. This is done with "raise" rather than "assert" so its behavior doesn't vary depending on "-O". - Don't pass process creation flags, because they were 0 except on Windows. - Don't fall back to SIGTERM if Python's signal module doesn't know about SIGKILL. This was specifically for Windows which has no SIGKILL. See #1756 for discussion.
1 parent f42a63b commit 2f017ac

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed
 

‎git/cmd.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -1015,11 +1015,9 @@ def execute(
10151015

10161016
def kill_process(pid: int) -> None:
10171017
"""Callback to kill a process."""
1018-
p = Popen(
1019-
["ps", "--ppid", str(pid)],
1020-
stdout=PIPE,
1021-
creationflags=PROC_CREATIONFLAGS,
1022-
)
1018+
if os.name == "nt":
1019+
raise AssertionError("Bug: This callback would be ineffective and unsafe on Windows, stopping.")
1020+
p = Popen(["ps", "--ppid", str(pid)], stdout=PIPE)
10231021
child_pids = []
10241022
if p.stdout is not None:
10251023
for line in p.stdout:
@@ -1028,18 +1026,16 @@ def kill_process(pid: int) -> None:
10281026
if local_pid.isdigit():
10291027
child_pids.append(int(local_pid))
10301028
try:
1031-
# Windows does not have SIGKILL, so use SIGTERM instead.
1032-
sig = getattr(signal, "SIGKILL", signal.SIGTERM)
1033-
os.kill(pid, sig)
1029+
os.kill(pid, signal.SIGKILL)
10341030
for child_pid in child_pids:
10351031
try:
1036-
os.kill(child_pid, sig)
1032+
os.kill(child_pid, signal.SIGKILL)
10371033
except OSError:
10381034
pass
10391035
kill_check.set() # Tell the main routine that the process was killed.
10401036
except OSError:
1041-
# It is possible that the process gets completed in the duration after timeout
1042-
# happens and before we try to kill the process.
1037+
# It is possible that the process gets completed in the duration after
1038+
# timeout happens and before we try to kill the process.
10431039
pass
10441040
return
10451041

0 commit comments

Comments
 (0)
Please sign in to comment.