Skip to content
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

[Windows] suggestion for os.kill(pid,CTRL_C_EVENT) in tests #55570

Open
iigor mannequin opened this issue Mar 1, 2011 · 5 comments
Open

[Windows] suggestion for os.kill(pid,CTRL_C_EVENT) in tests #55570

iigor mannequin opened this issue Mar 1, 2011 · 5 comments
Assignees
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes OS-windows tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@iigor
Copy link
Mannequin

iigor mannequin commented Mar 1, 2011

BPO 11361
Nosy @giampaolo, @briancurtin, @eryksun
Files
  • 1.patch: patch for this issue
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/briancurtin'
    closed_at = None
    created_at = <Date 2011-03-01.13:30:20.233>
    labels = ['type-bug', '3.8', '3.9', '3.10', 'tests', 'OS-windows']
    title = '[Windows] suggestion for os.kill(pid,CTRL_C_EVENT) in tests'
    updated_at = <Date 2021-03-08.20:13:15.284>
    user = 'https://bugs.python.org/iigor'

    bugs.python.org fields:

    activity = <Date 2021-03-08.20:13:15.284>
    actor = 'vstinner'
    assignee = 'brian.curtin'
    closed = False
    closed_date = None
    closer = None
    components = ['Tests', 'Windows']
    creation = <Date 2011-03-01.13:30:20.233>
    creator = 'iigor'
    dependencies = []
    files = ['20958']
    hgrepos = []
    issue_num = 11361
    keywords = ['patch']
    message_count = 5.0
    messages = ['129761', '134935', '134983', '222646', '255966']
    nosy_count = 4.0
    nosy_names = ['giampaolo.rodola', 'brian.curtin', 'iigor', 'eryksun']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue11361'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @iigor
    Copy link
    Mannequin Author

    iigor mannequin commented Mar 1, 2011

    Hello!

    I see some 'skip' in test_os.py

    @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 win_console_handler.py_orig

    26,27d25
    < elif sig == signal.SIGINT:
    < pass
    34,35d31
    < def ctrl_handler_int(sig, obj):
    < sys.exit(1)
    40d35
    < NULL = ctypes.POINTER(ctypes.c_int)()
    43,53d37
    < # 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
    OOL)
    < if not SetConsoleCtrlHandler(NULL, 0):
    < print("Unable to restore SetConsoleCtrlHandler")
    < exit(-1)
    <
    < signal.signal(signal.SIGINT, ctrl_handler_int)
    <
    < SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)
    57a42

    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

    OK

    @iigor iigor mannequin added topic-IO type-bug An unexpected behavior, bug, or error labels Mar 1, 2011
    @terryjreedy terryjreedy changed the title suggestion for os.kill(pid,CTRL_C_EVENT) suggestion for os.kill(pid,CTRL_C_EVENT) in tests Mar 4, 2011
    @briancurtin briancurtin self-assigned this Apr 30, 2011
    @vstinner
    Copy link
    Member

    vstinner commented May 1, 2011

    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.

    @vstinner
    Copy link
    Member

    vstinner commented May 2, 2011

    http://www.python.org/dev/buildbot/all/builders/x86%20XP-4%203.x/builds/4483/steps/test/logs/stdio
    ----------

    test test_os failed -- Traceback (most recent call last):
      File "D:\cygwin\home\db3l\buildarea\3.x.bolen-windows\build\lib\test\test_os.py", 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\test_os.py", line 1155, in _kill_with_event
        self.fail("subprocess did not stop on {}".format(name))
    AssertionError: subprocess did not stop on CTRL_BREAK_EVENT

    @gpshead
    Copy link
    Member

    gpshead commented Jul 10, 2014

    (un-cc'ing myself as I can't deal with Windows)

    @gpshead gpshead added tests Tests in the Lib/test dir OS-windows and removed topic-IO labels Jul 10, 2014
    @eryksun
    Copy link
    Contributor

    eryksun commented Dec 5, 2015

    test_CTRL_C_EVENT can be removed from Lib/test/test_os.py. 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
        ^BREAK
        >>> 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)
        1
        >>> test_ctrl_event(signal.CTRL_C_EVENT) # works
        ^C

    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.

    @eryksun eryksun added 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Feb 26, 2021
    @vstinner vstinner changed the title suggestion for os.kill(pid,CTRL_C_EVENT) in tests [Windows] suggestion for os.kill(pid,CTRL_C_EVENT) in tests Mar 8, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes OS-windows tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants