[issue42962] Windows: SystemError during os.kill(..., signal.CTRL_C_EVENT)

Eryk Sun report at bugs.python.org
Mon Jan 18 18:22:48 EST 2021


Eryk Sun <eryksun at gmail.com> added the comment:

> 4. In console A, type the PID from console B and execute the command.

In Windows, os.kill() is a rather confused function. In particular, it confuses a process ID (pid) with a process group ID (pgid). If the signal is CTRL_C_EVENT or CTRL_BREAK_EVENT, it first tries WinAPI GenerateConsoleCtrlEvent(). This function takes a pgid for a process group in the console session. Passing it a pid of a process in the console session that's not a pgid has undefined behavior (usually it acts like passing 0 as the pgid, but it's complicated; I have an open issue on Microsoft's GitHub repo for this that's been languishing for a couple years). Passing it a pid of a process that's not in the console session will fail as an invalid parameter. 

In the latter case, os.kill() sets an error but still tries the regular pid (not pgid) path of OpenProcess() and TerminateProcess(). If the latter succeeds, it returns success. However, it doesn't clear the error that was set as a result of the GenerateConsoleCtrlEvent() call, which causes SystemError to be raised.

>  Oddly, `echo %errorlevel%` will print `0` rather than `-1073741510` 

There's nothing odd about that. The value of CTRL_C_EVENT is 0. GenerateConsoleCtrlEvent() does not work across different console sessions. So os.kill() has simply opened a handle to the process and called TerminateProcess(handle, 0).

----------
nosy: +eryksun

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


More information about the Python-bugs-list mailing list