gh-149110: Fix race in _PyFrame_IsIncomplete for FRAME_OWNED_BY_FRAME… - #156001
Open
BHUVANSH855 wants to merge 2 commits into
Open
gh-149110: Fix race in _PyFrame_IsIncomplete for FRAME_OWNED_BY_FRAME…#156001BHUVANSH855 wants to merge 2 commits into
BHUVANSH855 wants to merge 2 commits into
Conversation
…_FRAME_OBJECT frames
markshannon
reviewed
Aug 19, 2026
| new_frame->instr_ptr = | ||
| _PyFrame_GetBytecode(new_frame) + code->_co_firsttraceable + 1; | ||
| } | ||
| /* Set owner BEFORE updating f->f_frame so any concurrent reader that |
Member
There was a problem hiding this comment.
Does the C memory model guarantee this?
Why can't another thread see f->f_frame = new_frame before new_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT?
Contributor
Author
There was a problem hiding this comment.
You're right, plain C stores provide no ordering guarantee. Fixed in the follow-up commit: take_ownership() now uses _Py_atomic_store_ptr_release() to publish f->f_frame, and PyFrame_GetBack() uses _Py_atomic_load_ptr_acquire() to read it. This establishes the required C11 happens-before relationship — any reader that observes the new f_frame pointer is guaranteed to also observe new_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT.
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.
_PyFrame_IsIncompletewas checkinginstr_ptragainst a TLBC slot onframes already owned by a frame object, which races with
take_ownership()when another thread walks
frame.f_back(e.g. via pdb).Fix:
_PyFrame_IsIncompleteforFRAME_OWNED_BY_FRAME_OBJECTframes —
take_ownership()already guarantees they're completetake_ownership()soowneris set beforef->f_frameis visible to concurrent readersco_tlbc->entries[idx]writes/readsAdded a regression test in
test_free_threading/test_frame.py.Fixes issue #149110
!_PyFrame_IsIncomplete(frame->f_frame)#149110