()
Cleanup threading module state that should not exist after a fork.
Cleanup threading module state that should not exist after a fork.
Cleanup threading module state that should not exist after a fork .
def _after_fork():
    """
    Cleanup threading module state that should not exist after a fork.
    """
    # Reset _active_limbo_lock, in case we forked while the lock was held
    # by another (non-forked) thread.  http://bugs.python.org/issue874900
    global _active_limbo_lock, _main_thread
    global _shutdown_locks_lock, _shutdown_locks
    _active_limbo_lock = RLock()

    # fork() only copied the current thread; clear references to others.
    new_active = {}

    try:
        current = _active[get_ident()]
    except KeyError:
        # fork() was called in a thread which was not spawned
        # by threading.Thread. For example, a thread spawned
        # by thread.start_new_thread().
        current = _MainThread()

    _main_thread = current

    # reset _shutdown() locks: threads re-register their _tstate_lock below
    _shutdown_locks_lock = _allocate_lock()
    _shutdown_locks = set()

    with _active_limbo_lock:
        # Dangling thread instances must still have their locks reset,
        # because someone may join() them.
        threads = set(_enumerate())
        threads.update(_dangling)
        for thread in threads:
            # Any lock/condition variable may be currently locked or in an
            # invalid state, so we reinitialize them.
            if thread is current:
                # There is only one active thread. We reset the ident to
                # its new value since it can have changed.
                thread._reset_internal_locks(True)
                ident = get_ident()
                thread._ident = ident
                new_active[ident] = thread
            else:
                # All the others are already stopped.
                thread._reset_internal_locks(False)
                thread._stop()

        _limbo.clear()
        _active.clear()
        _active.update(new_active)
        assert len(_active) == 1
def _after_fork ( ) : # Reset _active_limbo_lock, in case we forked while the lock was held # by another (non-forked) thread.  http://bugs.python.org/issue874900 global _active_limbo_lock , _main_thread global _shutdown_locks_lock , _shutdown_locks _active_limbo_lock = RLock ( ) # fork() only copied the current thread; clear references to others. new_active = { } try : current = _active [ get_ident ( ) ] except KeyError : # fork() was called in a thread which was not spawned # by threading.Thread. For example, a thread spawned # by thread.start_new_thread(). current = _MainThread ( ) _main_thread = current # reset _shutdown() locks: threads re-register their _tstate_lock below _shutdown_locks_lock = _allocate_lock ( ) _shutdown_locks = set ( ) with _active_limbo_lock : # Dangling thread instances must still have their locks reset, # because someone may join() them. threads = set ( _enumerate ( ) ) threads . update ( _dangling ) for thread in threads : # Any lock/condition variable may be currently locked or in an # invalid state, so we reinitialize them. if thread is current : # There is only one active thread. We reset the ident to # its new value since it can have changed. thread . _reset_internal_locks ( True ) ident = get_ident ( ) thread . _ident = ident new_active [ ident ] = thread else : # All the others are already stopped. thread . _reset_internal_locks ( False ) thread . _stop ( ) _limbo . clear ( ) _active . clear ( ) _active . update ( new_active ) assert len ( _active ) == 1