MCPcopy Index your code
hub / github.com/python/cpython / _after_fork

Function _after_fork

Lib/threading.py:1590–1632  ·  view source on GitHub ↗

Cleanup threading module state that should not exist after a fork.

()

Source from the content-addressed store, hash-verified

1588
1589
1590def _after_fork():
1591 """
1592 Cleanup threading module state that should not exist after a fork.
1593 """
1594 # Reset _active_limbo_lock, in case we forked while the lock was held
1595 # by another (non-forked) thread. http://bugs.python.org/issue874900
1596 global _active_limbo_lock, _main_thread
1597 _active_limbo_lock = RLock()
1598
1599 # fork() only copied the current thread; clear references to others.
1600 new_active = {}
1601
1602 try:
1603 current = _active[get_ident()]
1604 except KeyError:
1605 # fork() was called in a thread which was not spawned
1606 # by threading.Thread. For example, a thread spawned
1607 # by thread.start_new_thread().
1608 current = _MainThread()
1609
1610 _main_thread = current
1611
1612 with _active_limbo_lock:
1613 # Dangling thread instances must still have their locks reset,
1614 # because someone may join() them.
1615 threads = set(_enumerate())
1616 threads.update(_dangling)
1617 for thread in threads:
1618 # Any lock/condition variable may be currently locked or in an
1619 # invalid state, so we reinitialize them.
1620 if thread is current:
1621 # This is the one and only active thread.
1622 ident = get_ident()
1623 thread._after_fork(new_ident=ident)
1624 new_active[ident] = thread
1625 else:
1626 # All the others are already stopped.
1627 thread._after_fork()
1628
1629 _limbo.clear()
1630 _active.clear()
1631 _active.update(new_active)
1632 assert len(_active) == 1
1633
1634
1635if hasattr(_os, "register_at_fork"):

Callers

nothing calls this directly

Calls 7

_MainThreadClass · 0.85
setFunction · 0.85
_enumerateFunction · 0.85
RLockFunction · 0.70
updateMethod · 0.45
_after_forkMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…