[issue31863] Inconsistent returncode/exitcode for terminated child processes on Windows

Akos Kiss report at bugs.python.org
Thu Oct 26 08:59:59 EDT 2017


Akos Kiss <akosthekiss at gmail.com> added the comment:

A follow-up: in addition to `taskkill`, I've taken a look at another "official" way for killing processes, the `Stop-Process` PowerShell cmdlet (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/stop-process?view=powershell-5.1). Yet again, documentation is scarce on what the exit code of the terminated process will be. But PowerShell and .NET code base is open sourced, so I've dug a bit deeper and found that `Stop-Process` is based on `System.Diagnostics.Process.Kill()` (https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs#L1240), while `Process.Kill()` uses the `TerminateProcess` Win32 API (https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L93). Interestingly, `TerminateProcess` is called with -1 (this was surprising, to me at least, as exit code is unsigned on Windows AFAIK).

Therefore, I've added two new "kill" implementations to my original code experiment (wont repeat the whole code here, just the additions):

```py
def kill_with_taskkill(proc):
    print('kill child with taskkill /F')
    subprocess.run(['taskkill', '/F', '/pid', '%s' % proc.pid], check=True)

def kill_with_stopprocess(proc):
    print('kill child with powershell stop-process')
    subprocess.run(['powershell', 'stop-process', '%s' % proc.pid], check=True)
```

And I got:

```
run subprocess child with subprocess-taskkill
child process started with subprocess-taskkill
kill child with taskkill /F
SUCCESS: The process with PID 4024 has been terminated.
child terminated with 1
run subprocess child with subprocess-stopprocess
child process started with subprocess-stopprocess
kill child with powershell stop-process
child terminated with 4294967295

run multiprocessing child with multiprocessing-taskkill
child process started with multiprocessing-taskkill
kill child with taskkill /F
SUCCESS: The process with PID 5988 has been terminated.
child terminated with 1
run multiprocessing child with multiprocessing-stopprocess
child process started with multiprocessing-stopprocess
kill child with powershell stop-process
child terminated with 4294967295
```

My takeaways from the above are that
1) Windows is not consistent across itself,
2) 1 is not the only "valid" "terminated forcibly" exit code, and
3) negative exit code does not work, even if MS itself tries to use it.

BTW, I really think that killing a process with a code of 1 is questionable, as quite some apps return 1 themselves just to signal error (but proper termination). This makes it hard to tell applications' own error signaling and forced kills apart. But that's a personal opinion.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31863>
_______________________________________


More information about the Python-bugs-list mailing list