python / python/cpython

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

Offen
#134,644 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

3.13 3.14 3.15 interpreter-core type-crash
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in Python/bltinmodule.c bei builtin_input_impl und reproduziere die Assertion mit dem bereitgestellten Signal- und Eingabebeispiel. Verfolge, wie PyOS_Readline und PyErr_CheckSignals mit einer bereits vorhandenen Exception umgehen; abgeschlossen ist die Arbeit, wenn die Reproduktion nicht mehr mit der _PyObject_Call-Assertion abbricht und das beabsichtigte Interrupt-Verhalten beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
operating-systems
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.