FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-127041: Prevent new threads after an interpreter has started finalizing by ZeroIntensity · Pull Request #127044 · python/cpython · GitHub

/ cpython Public

gh-127041: Prevent new threads after an interpreter has started finalizing - #127044

Closed
ZeroIntensity wants to merge 6 commits into
python:mainfrom
ZeroIntensity:fix-interpreters-destroy
Closed

gh-127041: Prevent new threads after an interpreter has started finalizing#127044
ZeroIntensity wants to merge 6 commits into
python:mainfrom
ZeroIntensity:fix-interpreters-destroy

Conversation

ZeroIntensity commented Nov 19, 2024
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

cc @ericsnowcurrently

I decided to use a sentinel pointer instead of a new field like I originally did, which I think is a little better. I've left a few open issues as XXX comments :)

ZeroIntensity marked this pull request as draft November 20, 2024 01:40
Comment thread Python/pystate.c
Comment thread Python/pystate.c Outdated

Copy link
Copy Markdown
Member Author

I'm not too sure how I broke the threading tests with this.

Comment thread Python/pystate.c
Comment thread Python/pystate.c

Copy link
Copy Markdown
Member Author

As it turns out, the threading aborts are a result of a separate bug, I just managed to trigger it by raising in close. It seems that we don't clean up threads in a subinterpreter properly:

import _interpreters

interp = _interpreters.create()
source = """
import threading

def task():
    time.sleep(100)

t = threading.Thread(target=task)
t.start()
"""
_interpreters.run_string(interp, source)

This causes an assertion failure on my end:

python: Python/pystate.c:1969: tstate_activate: Assertion `!tstate->_status.bound_gilstate || tstate == gilstate_tss_get((tstate->interp->runtime))' failed.

I'm pretty sure that the problem is that even though the interpreter doesn't have a threads.main, the remaining threads.head that finalize_subinterpreters tries to use is still running, and trying to attach to it is a big no-no. The easiest fix would be to call wait_for_thread_shutdown much earlier for subinterpreters (probably where the main interpreter does it), but that doesn't fix anything for C threads, so maybe it's better if we come up with a more robust fix that waits for all threads using a _PySemaphore or something like that. Thoughts, @ericsnowcurrently?

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL