Skip to content

[io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored #72353

Closed

Description

@eryksun
BPO 28166
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba, @ZackerySpytz, @ValeriyaSinevich
PRs
  • bpo-28166: Continue to read after Ctrl+C if SIGINT was ignored #8344
  • gh-72353: WindowsConsoleIO misbehavior when Ctrl+C is ignored #17976
  • 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 = None
    closed_at = None
    created_at = <Date 2016-09-15.08:10:25.219>
    labels = ['interpreter-core', 'type-bug', '3.8', '3.9', 'expert-IO', '3.10', 'OS-windows']
    title = 'WindowsConsoleIO misbehavior when Ctrl+C is ignored'
    updated_at = <Date 2021-03-20.07:45:47.180>
    user = 'https://github.com/eryksun'

    bugs.python.org fields:

    activity = <Date 2021-03-20.07:45:47.180>
    actor = 'eryksun'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core', 'Windows', 'IO']
    creation = <Date 2016-09-15.08:10:25.219>
    creator = 'eryksun'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 28166
    keywords = ['patch']
    message_count = 3.0
    messages = ['276530', '321979', '322003']
    nosy_count = 7.0
    nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'ZackerySpytz', 'valer']
    pr_nums = ['8344', '17976']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28166'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Linked PRs

    Activity

    1. eryksun commented on Sep 15, 2016

      @eryksun
      ContributorAuthor

      _PyOS_WindowsConsoleReadline should continue to read when interrupted by Ctrl+C if SIGINT is ignored or the handler doesn't raise an exception. Currently it breaks out of the read loop, which looks like an EOF:

          >>> import signal
          >>> signal.signal(signal.SIGINT, signal.SIG_IGN)
          <built-in function default_int_handler>
          >>> input()
          Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
          EOFError 

      Also, in this case Ctrl+C quits the REPL.

      Similarly sys.stdin.buffer.raw should retry a read. Instead a read(1) returns an empty string and readall() raises an exception:

          >>> sys.stdin.buffer.raw.read(1)
          b''
      
          >>> sys.stdin.buffer.raw.read()
          Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
          OSError: [WinError 87] The parameter is incorrect
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Sep 15, 2016
    3. ValeriyaSinevich commented on Jul 20, 2018

      ValeriyaSinevichmannequin
      Mannequin

      I posted a PR but the problem with it is that all the input before the ignored Ctrl+C is lost.

    4. eryksun commented on Jul 20, 2018

      @eryksun
      ContributorAuthor

      all the input before the ignored Ctrl+C is lost

      The console host process doesn't know that Python is ignoring Ctrl+C, so it cancels the read and flushes the input buffer. For now, we have to accept this as a limitation of relying on the high-level console API (i.e. ReadConsoleW with the default settings), which implements command-line editing, aliases and history completely in the console host process. Restarting the read with a flushed input buffer is better than raising EOFError or exiting the REPL.

      In the future, Python's console readline implementation could hook deeper into the operation by temporarily disabling the console's ENABLE_PROCESSED_INPUT mode and using the pInputControl parameter to handle ^C and ^H (backspace). This would be fairly involved but still an imperfect solution because ^C will replace whichever character is under the cursor (fine if the cursor is at the end of the line, but otherwise pretty clunky). The only way to have complete control is to use the low-level ReadConsoleInput function. pyreadline implements this via ctypes.

    5. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      and removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Mar 20, 2021
    6. transferred this issue fromon Apr 10, 2022
    7. changed the title [-]WindowsConsoleIO misbehavior when Ctrl+C is ignored[/-] [+][io] _WindowsConsoleIO misbehaves when Ctrl+C is ignored[/+] on Aug 8, 2022
    8. added
      3.11only security fixes
      3.12only security fixes
      3.13only security fixes
      and removed on Jan 5, 2024
    9. added a commit that references this issue on Aug 29, 2026
    10. auwallpatil commented on Aug 29, 2026

      @auwallpatil
    11. added 2 commits that reference this issue on Aug 29, 2026
    12. added a commit that references this issue on Sep 12, 2026
    13. added a commit that references this issue on Oct 10, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixes3.12only security fixes3.13only security fixesOS-windowsinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-IOtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions