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