Skip to content

test(desktop): start every test subprocess without forking pytest - #2282

Merged
abrichr merged 9 commits into
mainfrom
ci/desktop-tests-no-fork
Nov 20, 2026
Merged

abrichr merged 9 commits into
mainfrom
ci/desktop-tests-no-fork

Conversation

@abrichr

@abrichr abrichr commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Builds on #2279 and includes its commits; merge #2279 first, or merge this one alone.

Why

#2277 stopped the release tests' git calls from forking pytest, and both macOS engine-test jobs then passed on main. One macOS job (135716931302, run 44568487040) still stopped for 45 minutes right after an ACL test in tests/test_engine/test_approver_anchor.py, whose next step runs /bin/chmod through subprocess.run. That job was cancelled, so it left no traceback; the cleanup step ended two leftover python3.12 processes, as in the earlier stalls. It is consistent with the same stall in a subprocess that is not git.

Cause

A forked child stops in close() while it closes inherited descriptors, because Tcl has started a thread in it. Three things have to be true, and all three were in the pytest process:

  1. tkinter is loaded. openadapt_capture.recorder in the published openadapt-capture 1.3.0 imports pympler, and pympler.util.compat imports tkinter. Collecting the tests is enough to load it.
  2. The process forks. Tcl registers a fork handler that starts its notifier thread in every forked child.
  3. The child closes descriptors. With the default close_fds=True, Python's forked child closes every inherited descriptor before exec. On the macOS 26 runners that close() sometimes never returns while Tcl's thread is in select().

Evidence from the macOS runners (macOS 26.7.2):

  • Run 44594236897 ran the suite as it was before ci(desktop): start git without forking in the release tooling and its tests #2277 under a supervisor that samples any forked copy of pytest alive for more than 20 seconds. It caught 12 stuck children in three jobs. All 24 samples show the same two threads:

    Thread (main)
      subprocess_fork_exec
        do_fork_exec
          child_exec
            _close_open_fds_maybe_unsafe
              close  (in libsystem_kernel.dylib)
    Thread
      NotifierThreadProc  (in libtcl9.0.dylib)
        __select  (in libsystem_kernel.dylib)
    
  • A standalone script that only imports tkinter and starts /usr/bin/true in a loop:

    Arm Starts Stalls
    fork, child closes descriptors (the Popen default) about 266,000 before the job's time limit 33
    fork, child closes nothing (close_fds=False with cwd) 400,000 0
    posix_spawn 400,000 0
  • The same script without tkinter, with or without the macOS frameworks loaded (run 44579317417): 1,600,000 forks and 800,000 spawns, 0 stalls.

That is about one stall per 8,000 forks. The suite forked tens of thousands of times before #2277 and 170 times after it, which matches most macOS jobs stalling before and one in six after.

What still forked

A recorder on the fork path, run over the whole Desktop suite on main (macOS 15, Python 3.12), counted 170 forks from 25 call sites:

Forks Where Why Python could not spawn
90 git in scripts/native_release.py and tests/test_native_release.py relative program, cwd, close_fds (fixed by #2279)
74 /bin/chmod in openadapt_desktop/approver_anchor.py (51), ffmpeg, and test helpers only the close_fds default
4 scripts/smoke_test_native_installer.py launch probe start_new_session
2 tests/test_wheel_layout.py cwd

Fix

tests/conftest.py now applies to the whole session, on Linux and macOS:

  • close_fds defaults to false for subprocesses started during the tests. Python then starts them with posix_spawn. Python opens its own descriptors close-on-exec, so a child still inherits only its standard streams. No engine code changes.
  • Any call that would still fork fails with the command and what to change, unless the test asks for the forking_allowed fixture.
  • The test summary prints subprocesses that forked pytest: N and each command.
  • The two wheel tests no longer pass cwd.
  • The three installer launch-probe tests use forking_allowed, because start_new_session cannot be spawned on Python 3.11 or 3.12. They account for 4 forks per session.

The per-module guard from #2277 and #2279 is replaced by this one. Windows has no fork and is left alone. Tests that check how the engine itself starts a process opt out with the python_subprocess_defaults fixture.

Not covered: the installed app

The installed engine can hit the same stall on macOS 26. Desktop's lock pins openadapt-capture 1.3.0 from PyPI, which imports pympler and so tkinter when the recorder is imported, and the engine then forks with the default close_fds for /bin/chmod (approver_anchor.py) and ffmpeg (presentation_export.py). At one stall per 8,000 forks and a few forks per session it would be rare, but a stalled call never returns. This pull request changes no shipped code. Two shipped fixes are possible and belong in their own pull requests: release the in-tree capture change that makes pympler optional (a0d1733) and raise Desktop's pin, and pass close_fds=False in the engine's own subprocess calls. Whether the frozen engine bundles tkinter was not checked.

Testing

  • Local, macOS 15, Python 3.12, whole Desktop suite: 2037 passed, 8 skipped, and the summary reports 4 forks (the launch-probe tests), down from 170. The two failures are the local-only ones explained in ci(desktop): keep native-release git calls and their tests from forking #2279 (released Flow 2.4.1 in the local environment).

  • check_release_intent.py passes; commit types are ci and test, so no version bump.

  • After the review at 9c53dea: tests/test_engine/test_child_process.py (from fix(desktop): start the engine's child processes without forking on macOS #2287) asks for a python_subprocess_defaults fixture that restores Python's real Popen behaviour, so the session default cannot make the engine's own no-fork tests pass whatever the engine does. With options.setdefault("close_fds", False) removed from openadapt_desktop/child_process.py, that module fails 3 tests on this branch, as it does on main. Dispatched Packages run 44634719141 at af43e78: all six engine-test jobs passed; both macOS jobs report 4 forks and 2055 passed, 9 skipped.

  • macOS CI before that: two dispatched Packages runs at 8781c29, 44608267378 and 44614364813. All twelve engine-test jobs passed (macOS, Windows and Linux on Python 3.11 and 3.12). Each of the four macOS jobs reports subprocesses that forked pytest: 4 and 2041 passed, 9 skipped, and none left an orphan process.

native_release.py now owns the git helper, and its three git calls and the
git calls in tests/test_native_release.py go through it. The fork guard
moves to conftest.py and covers both release test modules. The helper also
holds descriptors 0, 1 and 2 open for the length of a call when the process
was started with one closed, because Python otherwise falls back to fork.
…dows

Since #2277 the test helper returns git output without text-mode newline
translation. Two tests wrote a CRLF blob back with write_text, which on
Windows turned each CRLF into CR CR LF, so the reversion was no longer
exact and the feature commit stayed in the release intent. The tests now
write the blob's bytes back, and the helper normalizes line endings as the
text-mode call did.
…ows-newlines' into ci/desktop-native-release-no-fork
The session now defaults close_fds to false, which lets Python start a
child with posix_spawn, and fails any call that would still fork unless the
test asks for the forking_allowed fixture. The summary prints how many
subprocesses forked. The wheel tests no longer pass cwd, and the three
installer launch tests that need a new session are the only ones allowed
to fork.
@abrichr

abrichr commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 71ad7dc

Verdict: APPROVE. I reviewed the delta over #2279 (cf55f735, which I approved earlier) to this head: one merge-from-main commit (#2281) and 71ad7dc9. It changes only tests: conftest.py, test_wheel_layout.py and test_native_installer_smoke.py, and it drops the per-module no_subprocess_fork markers. No engine or script code changes beyond #2279.

What I checked

  • The close_fds default. Popen.__init__ is wrapped to set close_fds=False only when the caller didn't pass it, either positionally (it is the 8th positional after self, so len(args) < 8 is the right test) or by keyword, and didn't pass pass_fds. An explicit close_fds=True is respected and then fails loudly at _fork_exec, so it is never silently changed.
  • The guard. subprocess._fork_exec is looked up as a module global inside _execute_child, so the patch catches both the fork and the Linux vfork paths. Unless forking_allowed is set, it raises with the command and what to change. The guard is active only when subprocess._USE_POSIX_SPAWN is true, so Windows and libcs without it are untouched. patch.undo() runs at the end of the session.
  • Wheel tests. uv build <ROOT> replaces cwd=ROOT, and the probe now does os.chdir(dirname(site)), which is tmp_path, the same directory as before. -I still keeps PYTHONPATH and the cwd out.
  • Launch probe. The three _launch_probe tests opt in with forking_allowed because they use start_new_session. That matches the 4 forks reported.

Tests at this head (Linux, Python 3.12, whole Desktop suite)

  • 1891 passed, 63 skipped, 1 failed. The summary prints subprocesses that forked pytest: 4, all from the launch-probe tests, which matches the PR body.
  • The 1 failure is test_engine/test_dry_run.py::test_complete_dry_evidence_projects_codes_without_values. It fails the same way at main 1cf891e8e on this box (my environment has a Flow from another checkout installed), so this PR didn't cause it.
  • Not run: macOS and Windows. The macOS engine tests are the point of this PR, so the two dispatched macOS Packages runs promised in the PR body are the real evidence. I have not seen them.

P3s (non-blocking)

  1. Tests no longer cover the shipped default. During tests every engine Popen now runs with close_fds=False, while the installed engine uses Python's default (True). Python's own descriptors are close-on-exec, so this should be harmless. But an inheritable descriptor from a native library would now leak into children only under test. A long-lived child holding a pipe end could change EOF timing compared with production. One test that runs a representative engine call with the real default and forking_allowed would keep the shipped path covered.
  2. Scope of "any call that would fork". The guard covers subprocess (and asyncio, which uses it). It does not cover os.fork or a multiprocessing fork start method. Nothing in the Desktop tests or engine uses either today (rg finds none), so this is about wording only.
  3. As the PR says, the installed engine still forks for /bin/chmod and ffmpeg on macOS 26. That is a shipped-code question for a follow-up.

Generality gate

This is test infrastructure only. It names no application.

CI at this head

25 checks pass and 32 are skipped. The ubuntu desktop engine tests, release intent and desktop build all pass. The macOS and Windows engine jobs and the native installer were skipped on this PR event.

Merging is a maintainer decision. #2279 should merge first, as the PR says, or this PR alone. Wait for the macOS runs before merging.

# Conflicts:
#	packages/desktop/tests/conftest.py
@abrichr

abrichr commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: 9c53dea

Verdict: REQUEST CHANGES. At its own head the branch is correct, but merging it into current main (89883a4, which now includes #2287) breaks one of #2287's tests. It also stops #2287's no-fork tests from catching a regression. GitHub reports no textual conflict, and CI (25 pass) ran before #2287 landed, so neither one shows the problem.

P1: semantic conflict with #2287 (tests/test_engine/test_child_process.py)

  • I merged origin/main into 9c53dea locally (clean merge, 932ed0bcb) and ran the full packages/desktop/tests suite on py3.12 on Linux: 2 failed, 1903 passed, 63 skipped.
    • test_run_starts_a_child_without_forking fails with DID NOT RAISE AssertionError. Its negative control, a plain subprocess.run([sys.executable, "-c", "pass"]), is expected to fork. This branch's session fixture now defaults close_fds=False, so that call goes through posix_spawn and never reaches fork_refused. The test passes on main without this branch.
    • test_dry_run.py::test_complete_dry_evidence_projects_codes_without_values also fails on plain main, so it is not caused by this PR.
  • The guard loses its power. I made a mutant that removes options.setdefault("close_fds", False) from openadapt_desktop/child_process.py, which is the core of fix(desktop): start the engine's child processes without forking on macOS #2287.
    • On main, test_child_process.py catches it: 3 failed.
    • On the merge, with the negative control patched to pass close_fds=True, it catches nothing: 10 passed, 0 forks. The session-wide Popen.__init__ default does the helper's job for it, so a regression in the engine's real code path is no longer detected on Linux or macOS CI.
  • Suggested fix (the author's call):
    • Do not apply the session's close_fds default to tests that check the production defaults. For example, have fork_refused (or test_child_process.py) restore the real Popen.__init__ for its tests, or add a marker that opts a module out of the default.
    • Give the negative control an explicit close_fds=True.
    • Then merge main and re-run.
    • I checked that the close_fds=True change alone makes the merged module pass (10 passed). That change does not restore the mutant kill, so the opt-out is also needed.

What the coordinator asked me to check (all fine at 9c53dea):

  • The merge resolution did not drop a guard or a test. The diff against the merge-base with main (8776390) is the same 5 test files, and the def test_* sets in all 5 match main's exactly. The only lines removed are the two pytestmark = usefixtures("no_subprocess_fork") lines and the no_subprocess_fork fixture, which is replaced by the session fixture _subprocesses_do_not_fork plus forking_allowed.
  • The session fixture covers both release modules. Mutation probe: I appended a test that calls subprocess.run(["git","--version"], cwd=tmp_path) to test_native_release.py and then to test_release_intent.py. Each one fails with AssertionError: 'git --version' would fork pytest… from conftest.py:207.
  • Tests at the head: test_native_release.py, test_release_intent.py, test_wheel_layout.py, test_native_installer_smoke.py and test_no_console_windows.py all ran: 492 passed. 4 forks were reported, all of them the forking_allowed installer launch probes.

Generality gate: test-only change, with no workflow-, app- or vendor-specific code paths. The issue above is about the test harness hiding production regressions, not about generality.

Limits: Linux py3.12 only. I ran nothing on macOS or Windows, and I have no CI on the merged tree. I pushed nothing to the branch.

…ss defaults

The session's close_fds default made the engine's own no-fork tests pass
whatever the engine did. Those tests now ask for python_subprocess_defaults,
which restores Popen's real behaviour for them, and their negative control
passes close_fds=True explicitly.
@abrichr

abrichr commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: af43e78

Verdict: APPROVE. The P1 from my REQUEST CHANGES at 9c53dea is fixed at this head.

What changed since 9c53dea: main is merged in (8824529, which includes #2287 and #2290; current origin/main is an ancestor of the head). Commit af43e78 adds a python_subprocess_defaults fixture. test_child_process.py uses it for every test through pytestmark, and its negative control now passes close_fds=True.

Evidence (Linux, Python 3.12, exact head):

  • Full desktop suite: 1 failed, 1908 passed, 63 skipped. The only failure is test_dry_run.py::test_complete_dry_evidence_projects_codes_without_values, which also fails on main. test_run_starts_a_child_without_forking now passes. The session fork report lists 4 forks, all from the forking_allowed launch-probe tests.
  • Mutant 1 (the one you asked me to repeat): I replaced options.setdefault("close_fds", False) in openadapt_desktop/child_process.py with pass. The full suite then gave 4 failed: the 3 engine no-fork tests (test_run_starts_a_child_without_forking, test_popen_with_pipes_starts_a_child_without_forking, test_a_program_named_without_a_directory_is_found_on_path) plus the dry_run failure that already fails on main. At 9c53dea merged with main, this mutant made 0 tests fail. It is killed now.
  • Mutant 2 (generality, the release path): I forced close_fds=True in scripts/native_release.py's git runner. In the release test modules, 7 tests failed and 320 errored, all with "would fork pytest". The session guard still catches code that passes close_fds explicitly, so this fix didn't weaken it.
  • Both mutants were reverted, and the tree is clean at the head.

Review of the fixture:

  • The session fixture fills _REAL before any function fixture runs. python_subprocess_defaults uses function-scoped monkeypatch, so teardown restores the session patch and not Python's originals. Other tests keep the guard.
  • In test_child_process.py, fixtures from usefixtures run before argument fixtures. fork_refused therefore patches over the real _fork_exec, not under it. If that order were ever reversed, the negative control would fail loudly instead of passing silently.
  • On Windows, or wherever posix_spawn is unavailable, _REAL stays empty and the fixture does nothing, as intended.

CI at this head: green. That includes the desktop Python engine tests (ubuntu-latest, py3.12), the qualification contract, the sidecar build, packages-gate and CodeQL. The macOS and Windows native jobs were skipped by path scope.

P3 (not blocking):

  • close_fds=True in the negative control is redundant under real defaults, though harmless and explicit.
  • Nothing stops a future engine-behaviour test outside test_child_process.py from silently inheriting the session default. A comment pointing at python_subprocess_defaults covers this for now. The AST starter scan in test_child_process.py still stops engine modules from calling subprocess directly.

Limits: I ran this on Linux only; I didn't run it on macOS, where posix_spawn is the real production path. Merge and release decisions remain with the maintainers.

@abrichr
abrichr merged commit 43bf78d into main Nov 20, 2026
116 of 118 checks passed
@abrichr
abrichr deleted the ci/desktop-tests-no-fork branch November 20, 2026 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant