GeezerVison is showing github.com — gh-149816: Fix UAF on concurrent dict resize in free-threaded builds by sourabhligade · Pull Request #153712 · python/cpython
Conversation
sourabhligade
commented
Jul 14, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Summary
Fixes finding (124) from #149816: a use-after-free when a free-threaded dict is resized while another thread does lock-free lookups (including specialized LOAD_ATTR_WITH_HINT).
Root cause
On resize of a shared dict, the old PyDictKeysObject / values array is retained via QSBR so concurrent lock-free readers can still touch the table memory. Those tables previously kept live me_key / me_value pointers after ownership was transferred to the new table (or released on clear). A later update of the dict can free those objects while a reader still holds a stale table pointer and tries to incref them — UAF / ABA.
Fix
Structural hardening of the reclaim path, not a one-line band-aid:
dictkeys_clear_entries / free_keys_object / free_values: clear entry slots before QSBR-delayed free so stale readers cannot revive transferred objects.
dictkeys_decref: snapshot then null slots before XDECREF, so concurrent readers cannot follow a pointer we are about to free.
- Thread-safe key compares: a
NULL me_key under an active index is treated as DKIX_KEY_CHANGED (force locked retry) rather than a false miss — necessary so poisoning does not surface as spurious KeyError / AttributeError.
lookup_threadsafe_unicode and _LOAD_ATTR_WITH_HINT: re-validate ma_keys (and the slot value) after acquiring a reference, matching _Py_dict_lookup_threadsafe. Covers the deferred-refcount try-incref path that skips slot re-validation.
Tests
test_racing_load_attr_with_hint_and_resize
test_racing_dict_resize_and_lookup
Both stress concurrent resize + lock-free / specialized reads under free-threading (and TSAN in local builds).
Test plan
-
./python.exe -m unittest test.test_free_threading.test_dict.TestDict test.test_free_threading.test_races.TestRaces (×5, free-threaded + TSAN)
- Stress repro for attribute load vs resize (50 trials, 0 failures)
- CI free-threading / TSAN jobs on this PR
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…uilds
When a shared dict is resized, the old keys/values tables are retained via
QSBR so concurrent lock-free readers can still touch the memory. Those
tables previously kept live me_key/me_value pointers after ownership was
transferred or released, so a later update could free the objects while a
reader (including LOAD_ATTR_WITH_HINT) still held a stale table — a
use-after-free.
Clear entry slots before QSBR-delayed free, null before decref in
dictkeys_decref, re-validate ma_keys after acquiring a value in the hint
fast path and threadsafe unicode lookup, and treat a NULL me_key under an
active index as KEY_CHANGED so poisoned tables force a locked retry rather
than a false miss.
This addresses finding (124) from the free-threading race audit.
|
The following commit authors need to sign the Contributor License Agreement:

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
markshannon
Awaiting requested review from markshannon
markshannon is a code owner
methane
Awaiting requested review from methane
methane is a code owner
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.