Skip to content

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

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

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