Multithreaded subinterpreters can be running during finalization · Issue #126016 · python/cpython (original) (raw)

Crash report

What happened?

(Found while investigating a fix for #125920.)

Subinterpreters that are running in a different thread can still be running upon the finalization of the main interpreter, depending on what the thread was doing. Take the following script:

from threading import Thread import _interpreters

def silly(): interp = _interpreters.create() _interpreters.run_string(interp, "import time; time.sleep(100)")

if name == "main": Thread(target=silly).start()

On 3.13 and 3.14, killing the process with CTRL+C results in an assertion failure:

python: Python/pylifecycle.c:2480: finalize_subinterpreters: Assertion `!_PyInterpreterState_IsRunningMain(interp)' failed.

However, if the thread is daemon, then the interpreter fully segfaults:

from threading import Thread import _interpreters

def silly(): interp = _interpreters.create() _interpreters.run_string(interp, "import time; time.sleep(100)")

if name == "main": Thread(target=silly, daemon=True).start()

I'm going to investigate possible fixes.

cc @ericsnowcurrently

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Linux

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

No response

Linked PRs