GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@sourabhligade sourabhligade commented Jul 14, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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:

  1. dictkeys_clear_entries / free_keys_object / free_values: clear entry slots before QSBR-delayed free so stale readers cannot revive transferred objects.
  2. dictkeys_decref: snapshot then null slots before XDECREF, so concurrent readers cannot follow a pointer we are about to free.
  3. 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.
  4. 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
  • Issue: 22 free-threading race conditions #149816

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vedikatai
pythongh-149816: Fix UAF on concurrent dict resize in free-threaded b…
77ff29a
…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.
@sourabhligade
sourabhligade requested review from markshannon and methane as code owners July 14, 2026 18:35
@python-cla-bot

python-cla-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

  • [email protected]

CLA not signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot mentioned this pull request Jul 14, 2026
22 free-threading race conditions #149816
Open
13 of 24 tasks
@bedevere-app bedevere-app Bot added the awaiting review label Jul 14, 2026
@sourabhligade sourabhligade closed this Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@methane 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

Labels

awaiting review

Projects

None yet

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.

2 participants

@sourabhligade @vedikatai
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.