Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An interpreter can start running during _interpreters.destroy #127041

Open
ZeroIntensity opened this issue Nov 19, 2024 · 0 comments
Open

An interpreter can start running during _interpreters.destroy #127041

ZeroIntensity opened this issue Nov 19, 2024 · 0 comments
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir topic-subinterpreters type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@ZeroIntensity
Copy link
Member

ZeroIntensity commented Nov 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir topic-subinterpreters type-crash A hard crash of the interpreter, possibly with a core dump
Projects
Status: Todo
Development

No branches or pull requests

1 participant