这是indexloc提供的服务,不要输入任何密码
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

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

No one assigned

    Labels

    extension-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