File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -445,6 +445,27 @@ def background_thread(evt):
445445 self .assertEqual (out , b'' )
446446 self .assertEqual (err , b'' )
447447
448+ @unittest .skipUnless (hasattr (os , 'fork' ), "needs os.fork()" )
449+ def test_is_alive_after_fork (self ):
450+ # Try hard to trigger #18418: is_alive() could sometimes be True on
451+ # threads that vanished after a fork.
452+ old_interval = sys .getswitchinterval ()
453+ self .addCleanup (sys .setswitchinterval , old_interval )
454+
455+ # Make the bug more likely to manifest.
456+ sys .setswitchinterval (1e-6 )
457+
458+ for i in range (20 ):
459+ t = threading .Thread (target = lambda : None )
460+ t .start ()
461+ self .addCleanup (t .join )
462+ pid = os .fork ()
463+ if pid == 0 :
464+ os ._exit (1 if t .is_alive () else 0 )
465+ else :
466+ pid , status = os .waitpid (pid , 0 )
467+ self .assertEqual (0 , status )
468+
448469
449470class ThreadJoinOnShutdown (BaseTestCase ):
450471
Original file line number Diff line number Diff line change @@ -940,7 +940,7 @@ def _after_fork():
940940 new_active = {}
941941 current = current_thread ()
942942 with _active_limbo_lock :
943- for thread in _active . values ():
943+ for thread in _enumerate ():
944944 # Any lock/condition variable may be currently locked or in an
945945 # invalid state, so we reinitialize them.
946946 thread ._reset_internal_locks ()
Original file line number Diff line number Diff line change @@ -288,6 +288,7 @@ Ben Darnell
288288Kushal Das
289289Jonathan Dasteel
290290Pierre-Yves David
291+ A. Jesse Jiryu Davis
291292John DeGood
292293Ned Deily
293294Vincent Delft
Original file line number Diff line number Diff line change @@ -51,6 +51,9 @@ Core and Builtins
5151Library
5252-------
5353
54+ - Issue #18418: After fork(), reinit all threads states, not only active ones.
55+ Patch by A. Jesse Jiryu Davis.
56+
5457- Issue #17974: Switch unittest from using getopt to using argparse.
5558
5659- Issue #11798: TestSuite now drops references to own tests after execution.
You can’t perform that action at this time.
0 commit comments