Skip to content

Commit dea52bc

Browse files
committed
Revalidate dictionary insertion slots after preparation
Extract dictionary insertion and setdefault re-probing from #9439 with its five structural fixtures and whole canonical free-threading test. Regenerate the Lib file with scripts/update_lib from CPython d220409a13b77de7a6c30abe16ea90db01528edc. Assisted-by: OpenAI Codex: model version unavailable
1 parent 84d7afc commit dea52bc

2 files changed

Lines changed: 755 additions & 123 deletions

File tree

Lines changed: 360 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
import gc
2+
import time
3+
import unittest
4+
import weakref
5+
6+
from ast import Or
7+
from functools import partial
8+
from threading import Barrier, Thread
9+
from unittest import TestCase
10+
11+
try:
12+
import _testcapi
13+
except ImportError:
14+
_testcapi = None
15+
16+
from test.support import threading_helper
17+
18+
19+
@threading_helper.requires_working_threading()
20+
class TestDict(TestCase):
21+
def test_racing_creation_shared_keys(self):
22+
"""Verify that creating dictionaries is thread safe when we
23+
have a type with shared keys"""
24+
class C(int):
25+
pass
26+
27+
self.racing_creation(C)
28+
29+
def test_racing_creation_no_shared_keys(self):
30+
"""Verify that creating dictionaries is thread safe when we
31+
have a type with an ordinary dict"""
32+
self.racing_creation(Or)
33+
34+
def test_racing_creation_inline_values_invalid(self):
35+
"""Verify that re-creating a dict after we have invalid inline values
36+
is thread safe"""
37+
class C:
38+
pass
39+
40+
def make_obj():
41+
a = C()
42+
# Make object, make inline values invalid, and then delete dict
43+
a.__dict__ = {}
44+
del a.__dict__
45+
return a
46+
47+
self.racing_creation(make_obj)
48+
49+
def test_racing_creation_nonmanaged_dict(self):
50+
"""Verify that explicit creation of an unmanaged dict is thread safe
51+
outside of the normal attribute setting code path"""
52+
def make_obj():
53+
def f(): pass
54+
return f
55+
56+
def set(func, name, val):
57+
# Force creation of the dict via PyObject_GenericGetDict
58+
func.__dict__[name] = val
59+
60+
self.racing_creation(make_obj, set)
61+
62+
def racing_creation(self, cls, set=setattr):
63+
objects = []
64+
processed = []
65+
66+
OBJECT_COUNT = 100
67+
THREAD_COUNT = 10
68+
CUR = 0
69+
70+
for i in range(OBJECT_COUNT):
71+
objects.append(cls())
72+
73+
def writer_func(name):
74+
last = -1
75+
while True:
76+
if CUR == last:
77+
time.sleep(0.001)
78+
continue
79+
elif CUR == OBJECT_COUNT:
80+
break
81+
82+
obj = objects[CUR]
83+
set(obj, name, name)
84+
last = CUR
85+
processed.append(name)
86+
87+
writers = []
88+
for x in range(THREAD_COUNT):
89+
writer = Thread(target=partial(writer_func, f"a{x:02}"))
90+
writers.append(writer)
91+
writer.start()
92+
93+
for i in range(OBJECT_COUNT):
94+
CUR = i
95+
while len(processed) != THREAD_COUNT:
96+
time.sleep(0.001)
97+
processed.clear()
98+
99+
CUR = OBJECT_COUNT
100+
101+
for writer in writers:
102+
writer.join()
103+
104+
for obj_idx, obj in enumerate(objects):
105+
assert (
106+
len(obj.__dict__) == THREAD_COUNT
107+
), f"{len(obj.__dict__)} {obj.__dict__!r} {obj_idx}"
108+
for i in range(THREAD_COUNT):
109+
assert f"a{i:02}" in obj.__dict__, f"a{i:02} missing at {obj_idx}"
110+
111+
def test_racing_set_dict(self):
112+
"""Races assigning to __dict__ should be thread safe"""
113+
114+
def f(): pass
115+
l = []
116+
THREAD_COUNT = 10
117+
class MyDict(dict): pass
118+
119+
def writer_func(l):
120+
for i in range(1000):
121+
d = MyDict()
122+
l.append(weakref.ref(d))
123+
f.__dict__ = d
124+
125+
lists = []
126+
writers = []
127+
for x in range(THREAD_COUNT):
128+
thread_list = []
129+
lists.append(thread_list)
130+
writer = Thread(target=partial(writer_func, thread_list))
131+
writers.append(writer)
132+
133+
for writer in writers:
134+
writer.start()
135+
136+
for writer in writers:
137+
writer.join()
138+
139+
f.__dict__ = {}
140+
gc.collect()
141+
142+
for thread_list in lists:
143+
for ref in thread_list:
144+
self.assertIsNone(ref())
145+
146+
def test_racing_get_set_dict(self):
147+
"""Races getting and setting a dict should be thread safe"""
148+
THREAD_COUNT = 10
149+
barrier = Barrier(THREAD_COUNT)
150+
def work(d):
151+
barrier.wait()
152+
for _ in range(1000):
153+
d[10] = 0
154+
d.get(10, None)
155+
_ = d[10]
156+
157+
d = {}
158+
worker_threads = []
159+
for ii in range(THREAD_COUNT):
160+
worker_threads.append(Thread(target=work, args=[d]))
161+
for t in worker_threads:
162+
t.start()
163+
for t in worker_threads:
164+
t.join()
165+
166+
167+
def test_racing_set_object_dict(self):
168+
"""Races assigning to __dict__ should be thread safe"""
169+
class C: pass
170+
class MyDict(dict): pass
171+
for cyclic in (False, True):
172+
f = C()
173+
f.__dict__ = {"foo": 42}
174+
THREAD_COUNT = 10
175+
176+
def writer_func(l):
177+
for i in range(1000):
178+
if cyclic:
179+
other_d = {}
180+
d = MyDict({"foo": 100})
181+
if cyclic:
182+
d["x"] = other_d
183+
other_d["bar"] = d
184+
l.append(weakref.ref(d))
185+
f.__dict__ = d
186+
187+
def reader_func():
188+
for i in range(1000):
189+
f.foo
190+
191+
lists = []
192+
readers = []
193+
writers = []
194+
for x in range(THREAD_COUNT):
195+
thread_list = []
196+
lists.append(thread_list)
197+
writer = Thread(target=partial(writer_func, thread_list))
198+
writers.append(writer)
199+
200+
for x in range(THREAD_COUNT):
201+
reader = Thread(target=partial(reader_func))
202+
readers.append(reader)
203+
204+
for writer in writers:
205+
writer.start()
206+
for reader in readers:
207+
reader.start()
208+
209+
for writer in writers:
210+
writer.join()
211+
212+
for reader in readers:
213+
reader.join()
214+
215+
f.__dict__ = {}
216+
gc.collect()
217+
gc.collect()
218+
219+
count = 0
220+
ids = set()
221+
for thread_list in lists:
222+
for i, ref in enumerate(thread_list):
223+
if ref() is None:
224+
continue
225+
count += 1
226+
ids.add(id(ref()))
227+
count += 1
228+
229+
self.assertEqual(count, 0)
230+
231+
def test_racing_object_get_set_dict(self):
232+
e = Exception()
233+
234+
def writer():
235+
for i in range(10000):
236+
e.__dict__ = {1:2}
237+
238+
def reader():
239+
for i in range(10000):
240+
e.__dict__
241+
242+
t1 = Thread(target=writer)
243+
t2 = Thread(target=reader)
244+
245+
with threading_helper.start_threads([t1, t2]):
246+
pass
247+
248+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
249+
def test_racing_watch_unwatch_dict(self):
250+
# gh-148393: race between PyDict_Watch / PyDict_Unwatch
251+
# and concurrent dict mutation reading _ma_watcher_tag.
252+
wid = _testcapi.add_dict_watcher(0)
253+
try:
254+
d = {}
255+
ITERS = 1000
256+
257+
def writer():
258+
for i in range(ITERS):
259+
d[i] = i
260+
del d[i]
261+
262+
def watcher():
263+
for _ in range(ITERS):
264+
_testcapi.watch_dict(wid, d)
265+
_testcapi.unwatch_dict(wid, d)
266+
267+
threading_helper.run_concurrently([writer, watcher])
268+
finally:
269+
_testcapi.clear_dict_watcher(wid)
270+
271+
def test_racing_split_dict_clear_and_lookup(self):
272+
class C:
273+
pass
274+
275+
keys = [f"a{i}" for i in range(16)]
276+
277+
def make_split_nonembedded():
278+
inst = C()
279+
for key in keys:
280+
setattr(inst, key, keys.index(key))
281+
# dict.copy() of a split instance dict yields a split table
282+
# with non-embedded values
283+
return inst.__dict__.copy()
284+
285+
d = make_split_nonembedded()
286+
287+
def clearer():
288+
for _ in range(1000):
289+
d.clear()
290+
d.update(make_split_nonembedded())
291+
292+
def reader():
293+
for _ in range(1000):
294+
for k in keys:
295+
d.get(k)
296+
297+
threading_helper.run_concurrently([clearer, reader, reader])
298+
299+
def test_racing_embedded_values_clear_and_lookup(self):
300+
class C:
301+
pass
302+
303+
obj = C()
304+
def writer():
305+
for _ in range(1000):
306+
obj.x = 1
307+
obj.y = 2
308+
obj.z = 3
309+
obj.__dict__.clear()
310+
311+
def reader():
312+
for _ in range(1000):
313+
obj.__dict__.get('x')
314+
315+
threading_helper.run_concurrently([writer, reader, reader])
316+
317+
def test_racing_dict_update_and_method_lookup(self):
318+
# gh-144295: test race between dict modifications and method lookups.
319+
# Uses BytesIO because the race requires a type without Py_TPFLAGS_INLINE_VALUES
320+
# for the _PyDict_GetMethodStackRef code path.
321+
import io
322+
obj = io.BytesIO()
323+
324+
def writer():
325+
for _ in range(10000):
326+
obj.x = 1
327+
del obj.x
328+
329+
def reader():
330+
for _ in range(10000):
331+
obj.getvalue()
332+
333+
t1 = Thread(target=writer)
334+
t2 = Thread(target=reader)
335+
336+
with threading_helper.start_threads([t1, t2]):
337+
pass
338+
339+
def test_getstate_race_with_shared_keys(self):
340+
box = [None]
341+
enter = Barrier(2)
342+
leave = Barrier(2)
343+
344+
def reader():
345+
for _ in range(1000):
346+
enter.wait()
347+
box[0].__getstate__()
348+
leave.wait()
349+
350+
def writer():
351+
for i in range(1000):
352+
box[0] = type(f"C{i}", (), {})()
353+
enter.wait()
354+
setattr(box[0], str(i), 1)
355+
leave.wait()
356+
357+
threading_helper.run_concurrently([reader, writer])
358+
359+
if __name__ == "__main__":
360+
unittest.main()

0 commit comments

Comments
 (0)