python / python/cpython

An interpreter can start running during `_interpreters.destroy`

Open
#127,041 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

3.13 3.14 extension-modules topic-subinterpreters type-crash
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Crash report

What happened?

Now that gh-126914 is out of the way, we can try and deal with some of the other issues related to multithreading and subinterpreters that couldn't get dealt with before. Namely, the is_running_main inside _interpreters.destroy doesn't account for the fact that the interpreter could start running again by another thread after that check has already passed.

Quick reproducer:

from threading import Thread
import _interpreters

interp = _interpreters.create()


def run():
    _interpreters.run_string(interp, "1")
    _interpreters.destroy(interp)


threads = [Thread(target=run) for _ in range(1000)]
for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

I think the simplest fix would be to just disallow a subinterpreter from ever set "running main" again after that check has finished. I implemented this already in the (closed) gh-126696, but I'd like to try and refactor that a bit before just copying things over to a new PR.

cc @ericsnowcurrently, let me know if you have a better approach.

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

Linux

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

No response

Linked PRs
  • gh-127044

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the _interpreters.destroy path and its is_running_main check, then run the 1,000-thread reproducer using _interpreters.run_string and _interpreters.destroy. Review linked PR gh-127044 and the referenced closed gh-126696; done means the reproducer no longer permits an interpreter to start running during destruction.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.