You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This problem is explained in length in bpo-6721: "Locks in the standard library should be sanitized on fork".
The threading module registers an "at fork" callback: Thread._reset_internal_locks() is called to reset self._started (threading.Event) and self._tstate_lock. The current implementation creates new Python lock objects and forgets about the old ones.
I propose to add a new _at_fork_reinit() method to Python lock objects which reinitializes the native lock internally without having to create a new Python object.
Currently, my implementation basically creates a new native lock object and forgets about the old new (don't call PyThread_free_lock()).
Tomorrow, we can imagine a more efficient impementation using platform specific function to handle this case without having to forget about the old lock.
_thread.start_new_thread(fork_in_thread, ())
# block the main thread: fork_in_thread() exit the processtime.sleep(60)
Output:
---
parent process: threading.current_thread()=<_MainThread(MainThread, started 139879200077632)>
parent process: threading._main_thread=<_MainThread(MainThread, started 139879200077632)>
child process: threading.current_thread()=<_DummyThread(Dummy-1, started daemon 139878980245248)>
child process: threading._main_thread=<_DummyThread(Dummy-1, started daemon 139878980245248)>
---
My PR 19191 fix the issue:
---
parent process: threading.current_thread()=<_MainThread(MainThread, started 140583665170240)>
parent process: threading._main_thread=<_MainThread(MainThread, started 140583665170240)>
child process: threading.current_thread()=<_MainThread(MainThread, started 140583445075712)>
child process: threading._main_thread=<_MainThread(MainThread, started 140583445075712)>
---
Activity
vstinner commentedon Mar 27, 2020
Using a lock after fork() is unsafe and can crash.
Example of a crash in logging after a fork on AIX:
https://bugs.python.org/issue40068#msg365028
This problem is explained in length in bpo-6721: "Locks in the standard library should be sanitized on fork".
The threading module registers an "at fork" callback: Thread._reset_internal_locks() is called to reset self._started (threading.Event) and self._tstate_lock. The current implementation creates new Python lock objects and forgets about the old ones.
I propose to add a new _at_fork_reinit() method to Python lock objects which reinitializes the native lock internally without having to create a new Python object.
Currently, my implementation basically creates a new native lock object and forgets about the old new (don't call PyThread_free_lock()).
Tomorrow, we can imagine a more efficient impementation using platform specific function to handle this case without having to forget about the old lock.
vstinner commentedon Mar 27, 2020
_at_fork() has a bug: it creates a _DummyThread instead of a _MainThread. Example:
---
print(f"parent process: {threading.current_thread()=}")
print(f"parent process: {threading._main_thread=}")
Output:
---
parent process: threading.current_thread()=<_MainThread(MainThread, started 139879200077632)>
parent process: threading._main_thread=<_MainThread(MainThread, started 139879200077632)>
child process: threading.current_thread()=<_DummyThread(Dummy-1, started daemon 139878980245248)>
child process: threading._main_thread=<_DummyThread(Dummy-1, started daemon 139878980245248)>
---
My PR 19191 fix the issue:
---
parent process: threading.current_thread()=<_MainThread(MainThread, started 140583665170240)>
parent process: threading._main_thread=<_MainThread(MainThread, started 140583665170240)>
child process: threading.current_thread()=<_MainThread(MainThread, started 140583445075712)>
child process: threading._main_thread=<_MainThread(MainThread, started 140583445075712)>
---
vstinner commentedon Mar 27, 2020
New changeset d8ff44c by Victor Stinner in branch 'master':
bpo-40089: Fix threading._after_fork() (GH-19191)
d8ff44c
vstinner commentedon Mar 27, 2020
See also bpo-40091 "Crash in logging._after_at_fork_child_reinit_locks()" that I just created.
vstinner commentedon Mar 27, 2020
See also bpo-40092: "Crash in _PyThreadState_DeleteExcept() at fork in the process child".
vstinner commentedon Apr 7, 2020
New changeset 87255be by Victor Stinner in branch 'master':
bpo-40089: Add _at_fork_reinit() method to locks (GH-19195)
87255be
vstinner commentedon Apr 7, 2020
New changeset 6318e45 by Miss Islington (bot) in branch '3.8':
bpo-40089: Fix threading._after_fork() (GH-19191) (GH-19194)
6318e45
vstinner commentedon Apr 7, 2020
New changeset a514ccb by Miss Islington (bot) in branch '3.7':
bpo-40089: Fix threading._after_fork() (GH-19191) (GH-19193)
a514ccb