gh-151722: Defer GC tracking of frozendict.fromkeys() result until fully built#151967
Open
tonghuaroot wants to merge 1 commit into
Open
gh-151722: Defer GC tracking of frozendict.fromkeys() result until fully built#151967tonghuaroot wants to merge 1 commit into
tonghuaroot wants to merge 1 commit into
Conversation
…til fully built frozendict.fromkeys() built its result with PyIter_Next() on an already GC-tracked object, so a half-built frozendict was reachable from another thread (using the gc module) and could be observed mutating mid-construction in the free threading build. Untrack the result while it is being filled and re-track it once fully built.
302e646 to
d45e42d
Compare
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.
frozendict.fromkeys()fills its result by iterating the keys and insertingthem one at a time with
PyIter_Next(), which runs user code (the iterable's__next__). In the free-threaded build the result is GC-tracked during thisfill loop, so another thread calling
gc.get_objects()can reach and take areference to the still half-built frozendict. That breaks the
unique-reference invariant the lock-free insert relies on, tripping
assert(can_modify_dict(mp))on a debug build (or an unsynchronized writeconcurrent with a reader on a release build).
This mirrors gh-151740, which applied the same untrack-during-build /
track-when-complete pattern to
frozendict_new()andfrozendict_vectorcall().fromkeys()is the remaining construction path that change did not cover.This is the separate follow-up for the
fromkeys()path; in gh-151740@corona10 suggested submitting it as its own PR once that one was merged.
Issue: #151722