GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — multi-threading + fork warning when threads are stopped before fork · Issue #137109 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
multi-threading + fork warning when threads are stopped before fork#137109
Copy link
Assignees
gpshead
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@P403n1x87
P403n1x87
opened on Jul 25, 2025
Issue body actions

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

  • gh-137109: refactor warning about threads when forking #141438
  • [3.14] gh-137109: refactor warning about threads when forking (GH-141438) #141614
  • [3.13] gh-137109: refactor warning about threads when forking (GH-141438) (GH-141614) #141639

Activity

Next
  1. P403n1x87
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 25, 2025
  2. picnixz commented on Jul 25, 2025

    @picnixz
    picnixz
    on Jul 25, 2025
    Member
    More actions

    Let's remove the callback in the child. The order of execution would be:

    • Start thread.
    • Request to fork.
    • Join thread.
    • Do the fork.
    • Restart the thread in the parent.

    I think the problem is with the fact that thread from the parent process is not entirely joined. And so it's effectively the case that we're still multi-threaded even though it's not the case maybe.

    cc @ZeroIntensity as someone who has better threading knowledge.

  3. picnixz
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Jul 25, 2025
  4. P403n1x87 commented on Jul 25, 2025

    @P403n1x87
    P403n1x87
    on Jul 25, 2025
    ContributorAuthor
    More actions

    It looks to me that warn_about_fork_with_threads is warning on the new thread in the parent, because it is restarted by PyOS_AfterFork_Parent, before warn_about_fork_with_threads is called. So the warning is about the new thread running in the parent after the fork, rather than the original thread (that has actually been stopped and joined before the call to the fork syscall).

  5. colesbury commented on Jul 28, 2025

    @colesbury
    colesbury
    on Jul 28, 2025
    Contributor
    More actions

    Yeah, it'd be better to call the after-fork-parent calls after we check and warn about threads. That would avoid the spurious warning.

    Reacted by Gregory P. Smith
  6. gpshead commented on Nov 12, 2025

    @gpshead
    gpshead
    on Nov 12, 2025
    Member
    More actions

    Code paths that warn_about_fork_with_thread can take depend on PyOS_AfterFork_Parent() having been called already to avoid deadlock.

  7. gpshead commented on Nov 12, 2025

    @gpshead
    gpshead
    on Nov 12, 2025
    Member
    More actions

    A workaround for this could be for it to be called by PyOS_AfterFork_Parent() itself right before the run_at_forkers(...) call.

  8. gpshead commented on Nov 12, 2025

    @gpshead
    gpshead
    on Nov 12, 2025
    Member
    More actions

    But that'd still leave deadlocks possible as the Python world it may need to call into could rely on other afterfork parent calls that have been registered.

  9. gpshead commented on Nov 12, 2025

    @gpshead
    gpshead
    on Nov 12, 2025
    Member
    More actions

    Okay I think I have a solution for the most common case. The warn function can be split up into the OS specific APIs to get the number of threads (which can be called before AfterFork_Parent) and the Python APIs that must wait until after. This won't fix it in situations where to lack platform specific thread detection code or when those APIs fail, but will in the most widely used scenarios. PR coming after testing.

  10. gpshead
    added a commit that references this issue on Nov 12, 2025

    pythongh-137109: refactor warning about threads when forking

    a6d64b4
  11. gpshead commented on Nov 12, 2025

    @gpshead
    gpshead
    on Nov 12, 2025
    Member
    More actions

    re: race conditions, it may be OS specific as to if the thread could still show up in OS APIs as existing in the process after the internal C pthread_join() that the Python thread.join() did has returned. I'd like to think not, but that is up to the OS as there are no posix APIs to get a list of threads for a process so it is undefined behavior and up to the kernel implementation as to if it has finished tearing down that state and cleaning it up synchronously or not.

  12. gpshead
    self-assigned this
    on Nov 12, 2025
  13. gpshead
    added
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    on Nov 12, 2025
  14. gpshead
    added a commit that references this issue on Nov 13, 2025

    gh-137109: refactor warning about threads when forking (#141438)

    781cc68
  15. 7 remaining items

  16. miss-islington
    added a commit that references this issue on Nov 16, 2025

    [3.14] pythongh-137109: refactor warning about threads when forking (p…

    3b2b0a6
  17. gpshead
    added a commit that references this issue on Nov 17, 2025

    [3.13] gh-137109: refactor warning about threads when forking (GH-141438

    61a24ea
  18. gpshead commented on Nov 24, 2025

    @gpshead
    gpshead
    on Nov 24, 2025
    Member
    More actions

    The PRs merged should've fixed this for the most common case.

  19. gpshead
    closed this as completedon Nov 24, 2025
  20. StanFromIreland
    added a commit that references this issue on Dec 6, 2025

    pythongh-137109: refactor warning about threads when forking (python#…

    e452c92
  21. KotlinIsland
    added 3 commits that reference this issue on Sep 4, 2026

    fixed the last three ci found: 3.13 retainers, the fork warning, windows

    0820310

    asked the interpreter about the fork warning in the run being judged

    736224d

    stopped a race in cpython's own bookkeeping failing the fork parity

    db78a62
  22. bdarnell
    added 4 commits that reference this issue on Sep 20, 2026

    DEBUG: bracket gen_log.info()/os.fork() and add gc.collect()+sleep probe

    69a40ef

    DEBUG: measure OS-level thread count, not just PyThreadStates

    0147487

    test: ignore the spurious fork() multi-threading warning on macOS

    8ea9424

    test: ignore the spurious fork() multi-threading warning on x86_64 macOS

    e5ba878
  23. KotlinIsland
    added 3 commits that reference this issue on Sep 26, 2026

    fixed the last three ci found: 3.13 retainers, the fork warning, windows

    408e67d

    asked the interpreter about the fork warning in the run being judged

    4bafd35

    stopped a race in cpython's own bookkeeping failing the fork parity

    09a3dbf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @gpshead
    gpshead

Labels

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

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.