gh-144446: Fix thread safety of gi_frame, cr_frame and ag_frame in free-threading - #156037
Open
kumaraditya303 wants to merge 3 commits into
Open
gh-144446: Fix thread safety of gi_frame, cr_frame and ag_frame in free-threading#156037kumaraditya303 wants to merge 3 commits into
kumaraditya303 wants to merge 3 commits into
Conversation
… in free-threading build
3 tasks
kumaraditya303
commented
Aug 19, 2026
…e-144446.k3QzXa.rst
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
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.
Fixes thread safety of reading
gi_frame,cr_frameandag_framewhile the generator is running or finishing in another thread. Previously the reader could create a frame object for an interpreter frame that was concurrently being cleared, or two threads could each create a frame object for the same frame.The frame object is now created within the generator's critical section, re-checking the frame state after acquiring it so a finished generator returns
Noneinstead of a frame for a cleared iframe. Clearing the generator's frame ingen_clear_frame()andclear_gen_frame()now also holds the critical section so it cannot race with the getter.frame->frame_objis now set with a compare exchange as the running thread can create it without holding the generator's critical section (e.g. throughsys._getframe()) — the losing thread discards its frame object and uses the existing one — and it is cleared with an atomic exchange and read with an acquire load.