Skip to content

Starting a Process from "spawn" context has side effect of fixing start method to "fork" on Linux  #109263

Closed

Description

@Delgan

Bug report

Bug description:

Hello.

I observed that using a "spawn" context on Linux set the global start method to "fork", which is quite surprising to me:

import multiprocessing


def worker():
    pass


if __name__ == "__main__":
    spawn_context = multiprocessing.get_context("spawn")
    process = spawn_context.Process(target=worker)

    print("Start method before:", multiprocessing.get_start_method(allow_none=True))  # None
    process.start()
    process.join()
    print("Start method after:", multiprocessing.get_start_method(allow_none=True))  # "fork"

    multiprocessing.set_start_method("spawn")  # RuntimeError: context has already been set

One of the reasons for using get_context() is to prevent alterations to global state, as explicitly stated in the documentation.:

A library which wants to use a particular start method should probably use get_context() to avoid interfering with the choice of the library user.

I suspect the current behavior could potentially be a bug for the following reasons:

  1. It's unexpected to encounter "fork" as the global start method when employing a "spawn" context.
  2. This deviates from the documentation's assertion that using get_context() will not interfere with the user's choice, as it effectively prevents subsequent use of set_start_method().

If this behavior is intentional, I would like to suggest documenting this edge case for clarity.

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Sep 11, 2023
  2. gpshead commented on Sep 11, 2023

    @gpshead
    Member

    semi-related your other #109070 issue. 100% agreed that these multiprocessing APIs are not well behaved.

  3. aisk commented on Jun 22, 2025

    @aisk
    Member

    This is caused by the fact that the Popen implementation for the spawn and forkserver fetches the start_method using get_start_method(allow_none=False) in the main process and passes and set it to the child processes. However, get_start_method(allow_none=False) will also set the global start method using the default implementation.

    We can simply change allow_none to True, because in the child processes, the implementation can handle the case where start_method is None.

  4. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 22, 2025
  5. added a commit that references this issue on Dec 23, 2025
  6. added 2 commits that reference this issue on Dec 23, 2025
  7. gpshead commented on Dec 24, 2025

    @gpshead
    Member

    Thanks for the fix! merged & backports set to merge. We've recently started documenting this better, you'll find the current multiprocessing docs full of "If the global start method is not set and allow_none is False, the global start method is set to the default," with a link to the section about this gross global context state machine.

  8. added 2 commits that reference this issue on Dec 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-multiprocessingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions