GeezerVison is showing github.com — gh-152741: Publish thread states to the interpreter's thread list only after they are fully initialized by kumaraditya303 · Pull Request #156202 · python/cpython
Conversation
kumaraditya303
commented
Aug 21, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…st only after they are fully initialized
…er-tstate-publish
# Conflicts:
# Python/pystate.c
Comment on lines
-1718
to
-1720
|
|
// Add the new thread state to the interpreter.
|
|
|
PyThreadState *old_head = interp->threads.head;
|
|
|
add_threadstate(interp, (PyThreadState *)tstate, old_head);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codex flagged that moving add_threadstate() into bind_tstate() introduces a potential use after free caused by a race with interpreter shutdown. I think it's right. Here's the race:
- A new daemon thread, D, is started, but something causes a delay between thread creation and when it's bound.
- Another thread finalizes the interpreter, but because D's thread state hasn't been inserted into the thread list, finalization doesn't see it and doesn't set its state to
_Py_THREAD_SHUTTING_DOWN.
- D resumes. Its state is still
_Py_THREAD_DETACHED so it continues normal execution and attempts to dereference tstate->interp. This results in either interacting with a finalized interpreter (the main interpreter) or freed memory (a subinterpreter).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.