Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
gh-151722: Move frozendict free-threading regression test into test_d…
…ict.py
  • Loading branch information
tonghuaroot committed Jun 19, 2026
commit 47a49747f46962724560dbf9cd75e72d83c20c33
47 changes: 46 additions & 1 deletion Lib/test/test_free_threading/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ast import Or
from functools import partial
from threading import Barrier, Thread
from threading import Barrier, Event, Thread
from unittest import TestCase

try:
Expand Down Expand Up @@ -336,5 +336,50 @@ def reader():
with threading_helper.start_threads([t1, t2]):
pass

def test_racing_frozendict_reads_during_construction(self):
# gh-151722: a frozendict is GC-tracked before it is fully
# populated, so a half-built instance is observable from other
# threads. Reading it with len()/repr()/hash() must not race
# with the construction-time table and ma_used writes.
NUM_KEYS = 8192
NUM_ROUNDS = 40

latest = [frozendict()] # main -> reader handoff, never empty
done = Event()

class Evil:
def keys(self):
return [f"k{i}" for i in range(NUM_KEYS)]

def __getitem__(self, key):
if latest[0] is empty:
for obj in gc.get_objects():
if (isinstance(obj, frozendict)
and 0 < len(obj) < NUM_KEYS):
latest[0] = obj # leak the half-built object
break
return 1

empty = latest[0]

def reader():
while not done.is_set():
fd = latest[0]
len(fd)
repr(fd)
hash(fd)

readers = [Thread(target=reader) for _ in range(3)]
for t in readers:
t.start()
try:
for _ in range(NUM_ROUNDS):
latest[0] = empty
frozendict(Evil())
finally:
done.set()
for t in readers:
t.join()

if __name__ == "__main__":
unittest.main()
57 changes: 0 additions & 57 deletions Lib/test/test_free_threading/test_frozendict.py

This file was deleted.

Loading