@@ -347,6 +347,10 @@ def func():
347347
348348class StartNewThreadKwargsRace (unittest .TestCase ):
349349
350+ def setUp (self ):
351+ key = threading_helper .threading_setup ()
352+ self .addCleanup (threading_helper .threading_cleanup , * key )
353+
350354 @unittest .skipUnless (support .Py_GIL_DISABLED , "GIL must be disabled" )
351355 def test_dict_growsup_when_thread_start (self ):
352356 # See gh-149816 - (62) Concurrent kwargs growth causes heap overwrite
@@ -366,26 +370,28 @@ def mutator(shared, stop, prefix, burst):
366370 def nop (i , ** kwargs ):
367371 pass
368372
369- DELAY = 1.0
370- stop = thread .lock ()
371- shared = {f"base_{ i } " : i for i in range (20000 )}
372- n = 4
373- for i in range (n ):
374- args = (shared , stop , f"dynamic_{ i } " , 1000 )
375- thread .start_new_thread (mutator , args )
373+ with threading_helper .wait_threads_exit ():
374+ stop = thread .lock ()
375+ shared = {f"base_{ i } " : i for i in range (20000 )}
376+ n = 4
377+ for i in range (n ):
378+ args = (shared , stop , f"dynamic_{ i } " , 1000 )
379+ thread .start_new_thread (mutator , args )
380+
381+ snt = 32
382+ for i in range (snt ):
383+ try :
384+ thread .start_new_thread (nop , (i ,), shared )
385+ except RuntimeError :
386+ break
376387
377- snt = 32
378- for i in range (snt ):
379- try :
380- thread .start_new_thread (nop , (i ,), shared )
381- except RuntimeError :
382- break
383-
384- stop .acquire ()
385- # wait for all mutator threads stop.
386- wait_t = time .monotonic ()
387- while len (results ) < n and time .monotonic () - wait_t < DELAY :
388- time .sleep (0.01 )
388+ stop .acquire ()
389+ self .assertTrue (True , "test done" )
390+ # wait for all mutator threads stop.
391+ DELAY = 1.0
392+ wait_t = time .monotonic ()
393+ while len (results ) < n and time .monotonic () - wait_t < DELAY :
394+ time .sleep (0.001 )
389395
390396
391397class Barrier :
0 commit comments