Skip to content

multi-threading + fork warning when threads are stopped before fork#137109

@P403n1x87

Description

@P403n1x87

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

importosimportthreadingstop=Falsedeftarget(): whilenotstop: passthread=threading.Thread(target=target) defstop_thread(): globalstopstop=Truethread.join() defrestart_thread(): globalthread, stopstop=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() ifos.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

Linked PRs

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixes3.14bugs and security fixesextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions