-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terminate Windows processes with SIGTERM code rather than 0 #1150
Conversation
If the TerminateProcess WinAPI function is called with 0, then the exit code of the terminated process (e.g., observed by its parent) will be 0. However, this is usually associated with successful execution. Any other exit code could be used to signal forced termination, but perhaps the value of SIGTERM is the most meaningful.
A little background to this PR (it's a bit long, sorry). Based on (c)python 3 and psutil 5.4.0, I've conducted a little experiment. There are various ways of terminating a process in Python, and I wanted to see what can be observed by the parent afterwards. First, here is the script I used: import os
import psutil
import shlex
import signal
import subprocess
import sys
import time
def kill_with_os(proc):
print('kill with os.kill(pid,SIGTERM)')
os.kill(proc.pid, signal.SIGTERM)
def kill_with_subprocess(proc):
print('kill child with subprocess.Popen.terminate()')
proc.terminate()
def kill_with_psutil(proc):
print('kill child with psutil.Process(pid).terminate()')
psutil.Process(proc.pid).terminate()
def do_not_kill(proc):
pass
def run_and_kill_child(killfn, procarg):
print('run child with %s' % procarg)
with subprocess.Popen([sys.executable, __file__, procarg]) as proc:
time.sleep(1)
killfn(proc)
proc.wait()
print('child terminated with %s' % proc.returncode)
if __name__ == '__main__':
if len(sys.argv) < 2:
print('parent process started')
run_and_kill_child(kill_with_os, 'os')
run_and_kill_child(kill_with_subprocess, 'subprocess')
run_and_kill_child(kill_with_psutil, 'psutil')
run_and_kill_child(do_not_kill, 'return')
else:
print('child process started with %s' % sys.argv[1])
while sys.argv[1] != 'return':
pass So, it uses
And this is what I got on Windows 10:
It can be seen that on a Posix platform, Note: of course, it cannot be expected on Windows to get Note 2: the approach of the standard |
This seems reasonable. What happens if you |
On Windows, even at C level, only a handful of signal constants are available: Thus, the
And the doc of |
OK, thanks for looking into this. Merging. |
If the TerminateProcess WinAPI function is called with 0, then the
exit code of the terminated process (e.g., observed by its parent)
will be 0. However, this is usually associated with successful
execution. Any other exit code could be used to signal forced
termination, but perhaps the value of SIGTERM is the most
meaningful.