Skip to content

test: cover fork_context() darwin/win32 branch (no start-method fix at import) #1661

Closed

Description

@Jammy2211

Overview

PyAutoFit#1657 (community fix, discussion https://github.com/orgs/PyAutoLabs/discussions/27) changed fork_context() so that on macOS/Windows it returns the multiprocessing module instead of calling multiprocessing.get_context() with no argument — that no-arg call ran at import time and locked the default start method, so a later set_start_method(...) raised "context has already been set". Ubuntu CI never reaches the darwin/win32 branch, so the fix has no regression protection. This task adds tests that exercise that branch on Linux CI by monkeypatching sys.platform.

Plan

  • Add darwin-path tests to test_autofit/non_linear/test_fork_context.py: fork_context() returns the multiprocessing module and exposes Process, Queue, Pool.
  • Spy on multiprocessing.get_context on that path and assert it is never called with no argument.
  • Keep the existing Linux get_context("fork") assertion.
  • Witness: revert the final line of context.py to return multiprocessing.get_context() locally — the new test fails; restore — it passes.
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: feature/fork-context-darwin-test

Implementation Steps

  1. In test_autofit/non_linear/test_fork_context.py, add a test that uses monkeypatch.setattr(sys, "platform", "darwin") and asserts fork_context() is multiprocessing and that it has Process, Queue, Pool.
  2. Add a test that, on the darwin path, replaces multiprocessing.get_context with a recording spy (monkeypatch) and asserts no call was made with no argument (the pre-fix: do not fix the multiprocessing start method at import time on macOS #1657 import-time call).
  3. Keep test_fork_context_is_fork (Linux get_context("fork")) unchanged; monkeypatch restores all state so conftest's fork setting is unaffected.
  4. Witness: temporarily revert autofit/non_linear/parallel/context.py final line to return multiprocessing.get_context(); new tests fail; restore; they pass.
  5. Run python -m pytest test_autofit/non_linear/test_fork_context.py -q and the full test_autofit suite.

Key Files

  • autofit/non_linear/parallel/context.py — fork_context() (code under test, unchanged)
  • test_autofit/non_linear/test_fork_context.py — new tests

Links: PyAutoFit#1657, https://github.com/orgs/PyAutoLabs/discussions/27

Original Prompt

Click to expand starting prompt

PyAutoFit: add a regression test for fork_context() not fixing the multiprocessing start…

Type: bug
Target: PyAutoFit
Repos:

  • PyAutoFit
    Difficulty: small
    Autonomy: safe
    Priority: high
    Memory: wiki/lensing/sources/dark-matter-substructure.md; wiki/galaxies/sources/massive-ellipticals.md; wiki/galaxies/sources/cosmos-survey.md
    Status: formalised
    Consequence: glance
    Witness: a new test under test_autofit that monkeypatches sys.platform="darwin" and reloads parallel.context asserts multiprocessing.context._default_context._actual_context is None; it fails on the pre-fix: do not fix the multiprocessing start method at import time on macOS #1657 code (revert the context.py hunk locally) and passes on main
    Review-minutes: 3
    Unattended: ready

PyAutoFit: add a regression test for fork_context() not fixing the multiprocessing start method at import on darwin/win32.

Community PR PyAutoFit#1657 (@samlange04, discussion https://github.com/orgs/PyAutoLabs/discussions/27, merged 2026-10-07) changed autofit/non_linear/parallel/context.py so that on non-Linux platforms fork_context() returns the multiprocessing module instead of multiprocessing.get_context(), because the no-argument get_context() call at import time (parallel/process.py:47 builds class Process(fork_context().Process)) locked the interpreter's default start method and made any later set_start_method("spawn") raise "context has already been set". The PR has no test and Ubuntu CI cannot reach the darwin/win32 branch, so the fix is unprotected against regression.

Fix: add a test (next to test_fork_context.py) that monkeypatches sys.platform = "darwin", reloads autofit.non_linear.parallel.context and process, and asserts multiprocessing.context._default_context._actual_context is None afterwards (i.e. importing did not fix the start method), and that the returned object exposes .Process, .Queue and .Pool. Keep the existing Linux assertion (get_context("fork")) intact and restore the module state after the test so conftest's fork setting is unaffected.

Activity

  1. Jammy2211 commented on Oct 7, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR created: #1662 (test-only; 4 new darwin/win32 tests, witness red on the reverted #1657 hunk, full test_autofit 2965 passed / 2 skipped). No workspace impact. Awaiting human /prm.

  2. Jammy2211 commented on Oct 7, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped: merged in #1662 as 1e58fb1 (pending release). Closing.

  3. added a commit that references this issue on Oct 7, 2026
    94fd8fc
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions