Title: suggestion for os.kill(pid,CTRL_C_EVENT) in tests
Type: behavior Stage:
Components: Tests, Windows Versions: Python 3.6, Python 3.4, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: brian.curtin Nosy List: brian.curtin, eryksun, giampaolo.rodola, iigor, vstinner
Priority: normal Keywords: patch

Created on 2011-03-01 13:30 by iigor, last changed 2016-02-13 09:54 by giampaolo.rodola.

File name Uploaded Description Edit
1.patch iigor, 2011-03-01 13:36 patch for this issue
Messages (5)
msg129761 - (view) Author: Igor (iigor) Date: 2011-03-01 13:30

I see some 'skip' in

    @unittest.skip("subprocesses aren't inheriting CTRL+C property")
    def test_CTRL_C_EVENT(self):

I think, problem with calling SetConsoleCtrlHandler(NULL,0) in parent process, rather in child. If call SetConsoleCtrlHandler(NULL, 0).

Calling SetConsoleCtrlHandler(ctrl_handler, 1) is not enough. Need first call SetConsoleCtrlHandler(NULL, 0).

My patch for this test fixture: (may be, not so good, but it works)

H:\usr\python27\Lib\test>diff win_console_handler.py_orig

<     elif sig == signal.SIGINT:
<         pass
< def ctrl_handler_int(sig, obj):
<     sys.exit(1)
< NULL = ctypes.POINTER(ctypes.c_int)()
<     # Calling this with NULL and FALSE causes the calling process to
<     # handle CTRL+C, rather than ignore it. This property isn't inherited
<     # by subprocesses, if it's created with CREATE_NEW_PROCESS_GROUP flag
<     SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int), wintypes.B
<     if not SetConsoleCtrlHandler(NULL, 0):
<         print("Unable to restore SetConsoleCtrlHandler")
<         exit(-1)
<     signal.signal(signal.SIGINT, ctrl_handler_int)
<     SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)

And on my machine  (win2003 32bit):
test_CTRL_BREAK_EVENT (__main__.Win32KillTests) ... ok
test_CTRL_C_EVENT (__main__.Win32KillTests) ... ok
test_kill_int (__main__.Win32KillTests) ... ok
test_kill_sigterm (__main__.Win32KillTests) ... ok

Ran 61 tests in 2.344s

msg134935 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-01 22:25
Oh, if the process is able to handle CTRL+c on Windows, it means that faulthandler.register() could be used on Windows. While developing the faulthandler module, I tried all signals but I was only able to handle SIGSEGV, SIGABRT, SIGBUS and SIGILL on Windows. And all of these signals are reserved to faulthandler.enable() function. So faulthandler.register() is just not compiled on Windows.

If SetConsoleCtrlHandler() is really useful, we should maybe add something to the signal module to give access to this function.
msg134983 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-02 15:26
test test_os failed -- Traceback (most recent call last):
  File "D:\cygwin\home\db3l\buildarea\3.x.bolen-windows\build\lib\test\", line 1177, in test_CTRL_BREAK_EVENT
    self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
  File "D:\cygwin\home\db3l\buildarea\3.x.bolen-windows\build\lib\test\", line 1155, in _kill_with_event"subprocess did not stop on {}".format(name))
AssertionError: subprocess did not stop on CTRL_BREAK_EVENT
msg222646 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2014-07-10 00:09
(un-cc'ing myself as I can't deal with Windows)
msg255966 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015-12-05 19:13
test_CTRL_C_EVENT can be removed from Lib/test/ It's of no practical consequence. Ctrl+Break is always enabled in the child process, so test_CTRL_BREAK_EVENT should remain.

When using CREATE_NEW_PROCESS_GROUP, the child process is started with Ctrl+C disabled. Whether or not Ctrl+C is enabled in the parent process is irrelevant. 

An example that shows the intent of this creation flag is the /B (background) option of the cmd shell's "start" command. Ctrl+C from the user shouldn't interrupt such programs, so "start /B" uses the CREATE_NEW_PROCESS_GROUP creation flag. You can still kill the process with Ctrl+Break, assuming it hasn't installed a control handler that ignores CTRL_BREAK_EVENT instead of chaining to the default handler. 

For example:

    C:\>start /b /w py -3
    Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37)
    [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> import time
    >>> import signal
    >>> import ctypes
    >>> _ = signal.signal(signal.SIGINT, lambda *a: print('^C'))
    >>> _ = signal.signal(signal.SIGBREAK, lambda *a: print('^BREAK'))
    >>> def test_ctrl_event(event):
    ...     os.kill(os.getpid(), event)
    ...     time.sleep(1)

    >>> test_ctrl_event(signal.CTRL_BREAK_EVENT) # works
    >>> test_ctrl_event(signal.CTRL_C_EVENT) # nothing

As this issue notes, the child process can use ctypes to manually enable Ctrl+C events for the current process:

    >>> ctypes.windll.kernel32.SetConsoleCtrlHandler(None, 0)
    >>> test_ctrl_event(signal.CTRL_C_EVENT) # works

But this is contrived. You rarely have such control over the child process unless it's your own code, in which case there are far better IPC mechanisms available than to rely on the console host process (conhost.exe) as th arbiter of communication.
Date User Action Args
2016-02-13 09:54:23giampaolo.rodolasetnosy: + giampaolo.rodola
2015-12-05 19:13:25eryksunsetnosy: + eryksun

messages: + msg255966
versions: + Python 3.6
2014-07-10 00:09:13gregory.p.smithsetnosy: - gregory.p.smith
2014-07-10 00:09:01gregory.p.smithsetnosy: gregory.p.smith, vstinner, brian.curtin, iigor
messages: + msg222646
components: + Tests, Windows, - IO
2014-07-09 21:53:37BreamoreBoysetversions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
2011-05-02 15:26:16vstinnersetmessages: + msg134983
2011-05-01 22:25:10vstinnersetmessages: + msg134935
2011-04-30 23:17:46vstinnersetnosy: + vstinner
2011-04-30 02:59:29brian.curtinsetassignee: brian.curtin

nosy: + brian.curtin
2011-03-04 22:06:43terry.reedysettitle: suggestion for os.kill(pid,CTRL_C_EVENT) -> suggestion for os.kill(pid,CTRL_C_EVENT) in tests
2011-03-01 14:11:40pitrousetnosy: + gregory.p.smith

versions: + Python 3.2, Python 3.3
2011-03-01 13:36:43iigorsetfiles: + 1.patch
keywords: + patch
2011-03-01 13:30:20iigorcreate