Skip to content

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
python:mainfrom
kumaraditya303:gen-gi-frame-thread-safety
Open

gh-144446: Fix thread safety of gi_frame, cr_frame and ag_frame in free-threading#156037
kumaraditya303 wants to merge 3 commits into
python:mainfrom
kumaraditya303:gen-gi-frame-thread-safety

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Fixes thread safety of reading gi_frame, cr_frame and ag_frame while 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 None instead of a frame for a cleared iframe. Clearing the generator's frame in gen_clear_frame() and clear_gen_frame() now also holds the critical section so it cannot race with the getter. frame->frame_obj is now set with a compare exchange as the running thread can create it without holding the generator's critical section (e.g. through sys._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.

@bedevere-app bedevere-app Bot mentioned this pull request Aug 19, 2026
3 tasks
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-08-19-06-43-54.gh-issue-144446.k3QzXa.rst Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant