From 9cb9c5104fc9bc473309c65b87f6626b54256f30 Mon Sep 17 00:00:00 2001 From: samlange04 Date: Sat, 3 Oct 2026 20:46:58 +0800 Subject: [PATCH] fix: do not fix the multiprocessing start method at import time on macOS `fork_context()` (added in e6279c53f) returned `multiprocessing.get_context()` on macOS/Windows. Calling `get_context()` with no argument permanently fixes the interpreter's default start method, and `fork_context()` runs at import time via `class Process(fork_context().Process)`. Any code that imports autofit and then calls `multiprocessing.set_start_method(...)` therefore raised `RuntimeError: context has already been set`. The autofit test suite does exactly this on macOS in `test_autofit/conftest.py`, so `pytest` has failed to load the conftest on macOS since that commit. Return the `multiprocessing` module on those platforms instead. It exposes the same `Process` / `Queue` / `Pool` API but resolves the start method when a process is created, which is the pre-3.14 behaviour this helper exists to preserve. Non-macOS POSIX behaviour (pinned "fork") is unchanged. Co-Authored-By: Claude Fable 5.1 --- autofit/non_linear/parallel/context.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/autofit/non_linear/parallel/context.py b/autofit/non_linear/parallel/context.py index 781e6e4dc..02537aa8e 100644 --- a/autofit/non_linear/parallel/context.py +++ b/autofit/non_linear/parallel/context.py @@ -23,11 +23,25 @@ def fork_context(): restore old behaviour. This helper reproduces the pre-3.14 default on every platform. + On macOS and Windows the ``multiprocessing`` module itself is returned + rather than ``multiprocessing.get_context()``. Both expose the same + ``Process`` / ``Queue`` / ``Pool`` API, but calling ``get_context()`` with + no argument *fixes* the interpreter's default start method, and this + function runs at import time (``class Process(fork_context().Process)``). + That made ``multiprocessing.set_start_method("fork")`` raise + ``RuntimeError: context has already been set`` in any script or test + suite that imports autofit before choosing its start method (the autofit + test suite does exactly this on macOS in ``test_autofit/conftest.py``). + The module-level API defers the choice until a process actually starts, + which is the pre-3.14 behaviour this helper exists to preserve. + Returns ------- The "fork" multiprocessing context on POSIX platforms other than macOS, - else the platform default ("spawn" on Windows and macOS). + else the ``multiprocessing`` module, which dispatches to whichever start + method is current when a process is created ("spawn" by default on + Windows and macOS). """ if sys.platform != "darwin" and "fork" in multiprocessing.get_all_start_methods(): return multiprocessing.get_context("fork") - return multiprocessing.get_context() + return multiprocessing