1+ import gc
12import threading
23import unittest
34
45from concurrent .futures import ThreadPoolExecutor
56from threading import Thread
67from unittest import TestCase
78
8- from test .support import threading_helper
9+ from test .support import import_helper , threading_helper
10+
11+ _testinternalcapi = import_helper .import_module ("_testinternalcapi" )
912
1013
1114
1720class A :
1821 attr = 1
1922
23+ def world_stoppers ():
24+ """Finite workloads that contend for the stop-the-world mutex.
25+
26+ Waiting for the mutex releases the type lock, so that other threads
27+ can change types in the meantime. Use a short pause rather than full collections
28+ to prolong the contention without depending on the size of the heap.
29+ """
30+ def stopper ():
31+ for _ in range (100 ):
32+ _testinternalcapi .stop_the_world ()
33+
34+ def pauser ():
35+ for _ in range (100 ):
36+ _testinternalcapi .stop_the_world (0.0001 )
37+
38+ return [stopper , pauser ]
39+
40+
2041@threading_helper .requires_working_threading ()
2142class TestType (TestCase ):
2243 def test_attr_cache (self ):
@@ -182,9 +203,8 @@ def wrapper():
182203 reader .join ()
183204
184205 def test_setattr_many_subclasses (self ):
185- # gh-155978: Updating a special method queues a slot update for every
186- # affected subclass. Keep enough subclasses alive to require
187- # heap-allocated queue chunks in addition to the stack chunk.
206+ # gh-155978: Updating a special method updates the slot of every
207+ # affected subclass.
188208 class Base :
189209 pass
190210
@@ -201,6 +221,10 @@ def custom_repr(self):
201221 self .assertTrue (all (repr (cls ()) != "custom repr"
202222 for cls in subclasses ))
203223
224+ # The slot update must not keep references to the types.
225+ del subclasses
226+ gc .collect ()
227+ self .assertEqual (Base .__subclasses__ (), [])
204228
205229 def test_concurrent_setattr_deadlock (self ):
206230 # gh-155400: two threads assigning to a special method of the same
@@ -259,6 +283,151 @@ class B(A):
259283 with threading_helper .start_threads (threads ):
260284 pass
261285
286+ def check_stop_the_world_under_type_lock (self , setter ):
287+ # gh-158195: Python code that runs with the type lock held and stops
288+ # the world could deadlock with a thread that held the type lock
289+ # pinned while it waited for the stop-the-world mutex (__bases__
290+ # assignment). The first thread released the type lock while it was
291+ # blocked on the stop-the-world mutex, was handed the mutex, and then
292+ # waited for the type lock while owning the mutex.
293+ def basesetter ():
294+ class A :
295+ pass
296+ class B :
297+ pass
298+ class C (A ):
299+ pass
300+ for _ in range (100 ):
301+ C .__bases__ = (B ,)
302+ C .__bases__ = (A ,)
303+
304+ def stopper ():
305+ for _ in range (100 ):
306+ _testinternalcapi .stop_the_world ()
307+
308+ # The stopper keeps the stop-the-world mutex contended, which makes
309+ # the setters block on it. All workers have finite workloads so
310+ # that repeated invalidation cannot indefinitely delay a setter.
311+ threading_helper .run_concurrently (
312+ [setter , setter , basesetter , stopper ])
313+
314+ def test_stop_the_world_in_mro_deadlock (self ):
315+ # type_ready() calls a metaclass mro() with the type lock held.
316+ N = 50
317+
318+ class Meta (type ):
319+ def mro (cls ):
320+ _testinternalcapi .stop_the_world ()
321+ return type .mro (cls )
322+
323+ def setter ():
324+ for _ in range (N ):
325+ Meta ("K" , (), {})
326+
327+ self .check_stop_the_world_under_type_lock (setter )
328+
329+ def test_reentrant_mro_always_publishes (self ):
330+ # A custom mro() that updates a slot on every call must not make
331+ # the __bases__ assignment retry forever.
332+ class Other :
333+ pass
334+
335+ class Meta (type ):
336+ def mro (cls ):
337+ Other .__repr__ = lambda self : "x"
338+ return type .mro (cls )
339+
340+ class A :
341+ pass
342+
343+ class B :
344+ pass
345+
346+ class C (A , metaclass = Meta ):
347+ pass
348+
349+ C .__bases__ = (B ,)
350+ self .assertEqual (C .__mro__ , (C , B , object ))
351+
352+ def test_slot_update_skips_non_str_keys (self ):
353+ # Slots are computed (with the world stopped, for an update) without
354+ # comparing the slot name with non-str keys of the type dicts, which
355+ # could run user code. Here, that code would also update a slot
356+ # every time.
357+ calls = 0
358+
359+ class Other :
360+ pass
361+
362+ class Key :
363+ def __hash__ (self ):
364+ return hash ("__hash__" )
365+
366+ def __eq__ (self , other ):
367+ nonlocal calls
368+ calls += 1
369+ Other .__repr__ = lambda self : "x"
370+ return False
371+
372+ class Base :
373+ pass
374+
375+ with self .assertWarns (RuntimeWarning ):
376+ Sub = type ("Sub" , (Base ,), {Key (): None })
377+ self .assertEqual (calls , 0 )
378+
379+ Base .__hash__ = None
380+ self .assertEqual (calls , 0 )
381+ with self .assertRaises (TypeError ):
382+ hash (Sub ())
383+ del Base .__hash__
384+ hash (Sub ())
385+
386+ def test_concurrent_bases_assignment (self ):
387+ # Several threads reassign __bases__ of the same class, and of its
388+ # base, while others stop the world. The type lock is released while
389+ # waiting for the stop-the-world. At the end, the MRO must agree
390+ # with __bases__.
391+ N = 50
392+
393+ class A :
394+ def meth (self ):
395+ return "A"
396+ def __repr__ (self ):
397+ return "A"
398+ class B :
399+ def meth (self ):
400+ return "B"
401+ def __repr__ (self ):
402+ return "B"
403+ class C (A ):
404+ pass
405+ class D (C ):
406+ pass
407+
408+ def setter ():
409+ for _ in range (N ):
410+ C .__bases__ = (B ,)
411+ C .__bases__ = (A ,)
412+
413+ def subsetter ():
414+ for _ in range (N ):
415+ D .__bases__ = (C , B )
416+ D .__bases__ = (C ,)
417+
418+ threading_helper .run_concurrently (
419+ [setter , setter , subsetter ] + world_stoppers ())
420+
421+ self .assertEqual (C .__bases__ , (A ,))
422+ self .assertEqual (C .__mro__ , (C , A , object ))
423+ self .assertEqual (D .__bases__ , (C ,))
424+ self .assertEqual (D .__mro__ , (D , C , A , object ))
425+ self .assertEqual (D ().meth (), "A" )
426+ self .assertEqual (repr (C ()), "A" )
427+ self .assertEqual (repr (D ()), "A" )
428+ self .assertIn (C , A .__subclasses__ ())
429+ self .assertNotIn (C , B .__subclasses__ ())
430+
262431
263432if __name__ == "__main__" :
264433 unittest .main ()
0 commit comments