Cleanup threading module state that should not exist after a fork.
()
| 1588 | |
| 1589 | |
| 1590 | def _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 | |
| 1635 | if hasattr(_os, "register_at_fork"): |
nothing calls this directly
no test coverage detected
searching dependent graphs…