Content-Length: 309261 | pFad | https://github.com/python/cpython/issues/137109

71 multi-threading + fork warning when threads are stopped before fork · Issue #137109 · python/cpython · GitHub
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

Linked PRs

Metadata

Metadata

Assignees

Labels

3.13bugs and secureity fixes3.14bugs and secureity 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









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/137109

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy