Skip to content

Builtin input does not always correctly handle errors from PyOS_Readline#134644

@duaneg

Description

@duaneg

Assertion failure sending SIGINT while calling input

importos, signal, threadingsend=threading.Event() definterrupter(): whileTrue: send.wait() os.kill(os.getpid(), signal.SIGINT) threading.Thread(target=interrupter).start() send.set() whileTrue: try: input(">") exceptKeyboardInterrupt: pass

Note, this is a modified version of the example code in #112585.

Python (tested v3.12.10, v3.13.3, v3.14.0b1, and current HEAD), built with assertions enabled:

~/src/cpython/ $ ./python crash.py >>>>>>>>>>>python: Objects/call.c:342: _PyObject_Call: Assertion `!_PyErr_Occurred(tstate)' failed. Aborted (core dumped) 

IIUC the issue is builtin_input_impl in Python/bltinmodule.c calls PyOS_Readline but assumes it cannot set an exception:

 s = PyOS_Readline(stdin, stdout, promptstr); if (s == NULL){PyErr_CheckSignals(); if (!PyErr_Occurred()) PyErr_SetNone(PyExc_KeyboardInterrupt); 

If PyOS_Readline sets an exception (e.g. as in this case by running an interrupt signal handler itself), as opposed to being interrupted by a signal and returning NULLwithout setting the exception, and another signal with a handler is pending when PyErr_CheckSignals() is called, the handler will be called with the prior exception already set, and hence the assertion fails.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions