-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Open
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
If all threads are stopped before a fork, and then restarted in the parent (and optionally the child) process, the new multi-threading with fork warning is given
import os
import threading
stop = False
def target():
while not stop:
pass
thread = threading.Thread(target=target)
def stop_thread():
global stop
stop = True
thread.join()
def restart_thread():
global thread, stop
stop = False
(thread := threading.Thread(target=target)).start()
os.register_at_fork(before=stop_thread, after_in_child=restart_thread, after_in_parent=restart_thread)
thread.start()
if os.fork() == 0:
os._exit(0)
If the thread is not restarted in the parent, no warning is given. I believe warn_about_fork_with_threads
should be refactored so that a warning is given to the user only if there are threads detected after the call to PyOS_BeforeFork
. If it is not possible to give a warning there, as stated in the comments, perhaps one can just do the thread count check there and take note of the result, then warn after PyOS_AfterFork_Parent
, when safe to do so.
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error