python / python/cpython

Builtin input does not always correctly handle errors from `PyOS_Readline`

Abierto
#134,644 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

3.13 3.14 3.15 interpreter-core type-crash
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Assertion failure sending SIGINT while calling input
import os, signal, threading

send = threading.Event()

def interrupter():
    while True:
        send.wait()
        os.kill(os.getpid(), signal.SIGINT)

threading.Thread(target=interrupter).start()

send.set()
while True:
    try:
        input(">")
    except KeyboardInterrupt:
        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 NULL without 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
  • gh-134645

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en Python/bltinmodule.c, en builtin_input_impl, y reproduce la aserción con el ejemplo de señal y entrada proporcionado. Traza cómo PyOS_Readline y PyErr_CheckSignals manejan una excepción existente; se considera terminado cuando la reproducción ya no aborta con la aserción de _PyObject_Call y conserva el comportamiento de interrupción previsto.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
operating-systems
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.