@@ -590,7 +590,7 @@ def __repr__(self):
590590 return f"<{ cls .__module__ } .{ cls .__qualname__ } at { id (self ):#x} : { status } >"
591591
592592 def _at_fork_reinit (self ):
593- # Private method called by Thread._reset_internal_locks ()
593+ # Private method called by Thread._after_fork ()
594594 self ._cond ._at_fork_reinit ()
595595
596596 def is_set (self ):
@@ -936,11 +936,15 @@ class is implemented.
936936 # For debugging and _after_fork()
937937 _dangling .add (self )
938938
939- def _reset_internal_locks (self , is_alive ):
940- # private! Called by _after_fork() to reset our internal locks as
941- # they may be in an invalid state leading to a deadlock or crash.
939+ def _after_fork (self , new_ident = None ):
940+ # Private! Called by threading._after_fork().
942941 self ._started ._at_fork_reinit ()
943- if is_alive :
942+ if new_ident is not None :
943+ # This thread is alive.
944+ self ._ident = new_ident
945+ if self ._handle is not None :
946+ self ._handle .after_fork_alive ()
947+ assert self ._handle .ident == new_ident
944948 # bpo-42350: If the fork happens when the thread is already stopped
945949 # (ex: after threading._shutdown() has been called), _tstate_lock
946950 # is None. Do nothing in this case.
@@ -950,11 +954,14 @@ def _reset_internal_locks(self, is_alive):
950954 if self ._join_lock is not None :
951955 self ._join_lock ._at_fork_reinit ()
952956 else :
953- # The thread isn't alive after fork: it doesn't have a tstate
957+ # This thread isn't alive after fork: it doesn't have a tstate
954958 # anymore.
955959 self ._is_stopped = True
956960 self ._tstate_lock = None
957961 self ._join_lock = None
962+ if self ._handle is not None :
963+ self ._handle .after_fork_dead ()
964+ self ._handle = None
958965
959966 def __repr__ (self ):
960967 assert self ._initialized , "Thread.__init__() was not called"
@@ -1707,15 +1714,13 @@ def _after_fork():
17071714 # Any lock/condition variable may be currently locked or in an
17081715 # invalid state, so we reinitialize them.
17091716 if thread is current :
1710- # There is only one active thread. We reset the ident to
1711- # its new value since it can have changed.
1712- thread ._reset_internal_locks (True )
1717+ # This is the one and only active thread.
17131718 ident = get_ident ()
1714- thread ._ident = ident
1719+ thread ._after_fork ( new_ident = ident )
17151720 new_active [ident ] = thread
17161721 else :
17171722 # All the others are already stopped.
1718- thread ._reset_internal_locks ( False )
1723+ thread ._after_fork ( )
17191724 thread ._stop ()
17201725
17211726 _limbo .clear ()
0 commit comments