-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add _at_fork_reinit() method to locks #84270
Comments
Using a lock after fork() is unsafe and can crash. Example of a crash in logging after a fork on AIX: 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. |
_at_fork() has a bug: it creates a _DummyThread instead of a _MainThread. Example: import os, _thread, threading, time
def fork_in_thread():
pid = os.fork()
if pid:
# parent
os._exit(0)
print(f"parent process: {threading.current_thread()=}") _thread.start_new_thread(fork_in_thread, ())
# block the main thread: fork_in_thread() exit the process
time.sleep(60) Output: My PR 19191 fix the issue: |
See also bpo-40091 "Crash in logging._after_at_fork_child_reinit_locks()" that I just created. |
See also bpo-40092: "Crash in _PyThreadState_DeleteExcept() at fork in the process child". |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: