Eryk Sun <eryk...@gmail.com> added the comment:

FYI, Windows is not a POSIX operating system and does not implement Unix 
signals in the kernel. That said, the C language serves as a base level of 
cross-platform support for language runtimes across most operating systems, and 
there's significant coupling between C and Unix. Standard C requires six Unix 
signals to be supported in some fashion: SIGABRT, SIGTERM, SIGFPE, SIGILL, 
SIGSEGV, and SIGINT. 

The C runtime in Windows implements SIGABRT and SIGTERM only within the 
process, e.g. with C raise() and abort(). There's no mechanism to send these 
signals to another process. It implements SIGFPE, SIGILL, SIGSEGV using an OS 
structured exception handler, but these can't and shouldn't be handled in 
Python. 

That leaves SIGINT, which is implemented for console applications using a 
console control handler for the cancel event (CTRL_C_EVENT). The same handler 
also implements non-standard SIGBREAK for the other console control events: 
break, close, logoff, and shutdown. 

There's limited support to send the cancel and break events (CTRL_C_EVENT and 
CTRL_BREAK_EVENT) to other processes via WinAPI 
GenerateConsoleCtrlEvent(ctrlEvent, processGroupId). The event can be sent to a 
process group in the current console session or to all processes (group 0) in 
the console session. The process group ID (pgid) of a process cannot be 
directly queried. The only way to know a pgid is to create a process as the 
leader of a new group with the creation flag CREATE_NEW_PROCESS_GROUP, in which 
case the pgid is the pid. A new process group initially has the cancel event 
ignored, until a process manually enables it. So in practice you can only rely 
on sending the break event. This is implemented in Python via os.kill(pgid, 
event) and subprocess.Popen.send_signal(event). But remember the target pgid 
must be either 0 or a known pgid. Using a pid that's not a pgid has undefined 
behavior.

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

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42959>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to