Skip to content

fix(desktop): start the engine's child processes without forking on macOS - #2287

Merged
abrichr merged 2 commits into
mainfrom
fix/desktop-engine-spawn-subprocesses
Nov 19, 2026
Merged

abrichr merged 2 commits into
mainfrom
fix/desktop-engine-spawn-subprocesses

Conversation

@abrichr

@abrichr abrichr commented Nov 19, 2026

Copy link
Copy Markdown
Member

The installed engine can wait forever when it starts a helper process on macOS 26. This makes the engine's own helper processes start without a fork, and bumps Desktop to 0.30.5.

Cause

A child that Python forks stops in close() while it closes inherited descriptors, when Tcl is loaded in the parent. Tcl starts its notifier thread in every forked child. On the hosted macOS 26 runners, a script that only imports tkinter stalled 33 times in about 266,000 forks; without tkinter, or with posix_spawn, it did not stall in 400,000 starts or more (evidence and samples in #2282).

The engine loads Tcl: the published openadapt-capture 1.3.0 imports Pympler, which imports tkinter, when the recorder is imported. The 0.30.3 macOS build log shows PyInstaller bundling _tkinter (job 135798832682: "Processing standard module hook 'hook-_tkinter.py'", "Including run-time hook 'pyi_rth__tkinter.py'").

Fix

openadapt_desktop/child_process.py adds run and popen. On POSIX they set close_fds=False and resolve a bare program name to its absolute path, which are the conditions under which Python starts the child with posix_spawn. Python opens its own descriptors close-on-exec, so the child still inherits only its standard streams. They refuse cwd, start_new_session, preexec_fn and pass_fds, each of which would bring the fork back. On Windows they pass everything through unchanged.

The engine's process starts now go through them:

  • the Flow CLI, in flow_bridge.py (the default runner and popen)
  • /bin/chmod in approver_anchor.py
  • ffprobe and ffmpeg in presentation_export.py
  • the attended console and taskkill in portal/service.py

Tests

tests/test_engine/test_child_process.py:

  • fails if any module under openadapt_desktop/ refers to subprocess.run, subprocess.Popen and the like, or to os.system, os.popen, os.fork or os.spawn*, outside child_process.py
  • with the fork path replaced by a failure, run and popen (with pipes) still start a child, a bare program name is found on PATH, and a missing program raises FileNotFoundError
  • the four options that force a fork are refused

A recorder on the fork path, run over the Desktop suite locally (macOS 15, Python 3.12, without the release-intent module): 56 forks came from engine modules on main, and none do with this change. That run had 1,659 passed and the two local-only failures explained in #2279; a third failure in it, a portal test that patched subprocess.run, is fixed here.

Not covered

  • webbrowser.open in auth/browser_pkce.py starts its helper inside the standard library, once per sign-in, and still forks.
  • openadapt-capture starts its own recorder processes with multiprocessing, which forks in the same way. Removing tkinter from capture is the fix for that (release(capture): 1.4.0 #2286, then a Desktop pin raise).
  • macOS CI does not run on pull requests. The Windows and macOS engine tests first run on main.

Release

A fix commit to shipped code, so scripts/prepare_release.py bumped the version to 0.30.5; check_release_intent.py reports "Desktop release intent is consistent."

…acOS

With Tcl loaded in the engine, a child that Python forks on macOS 26 can
stop for good while it closes inherited descriptors, and the engine then
waits inside Popen with no timeout that applies. The engine's own child
processes (the Flow CLI, chmod, ffprobe and ffmpeg, the portal console) now
start through openadapt_desktop.child_process, which lets Python use
posix_spawn. A test fails if an engine module starts a process directly.
@abrichr

abrichr commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 862dadc

Are any engine subprocess paths still on fork?

  • I ran the whole Desktop suite (tests/, without e2e) on Linux with Python 3.12, with a recorder wrapped around subprocess._fork_exec that logs any call whose stack goes through openadapt_desktop/. glibc Python uses posix_spawn under the same conditions as macOS, so the recorder is valid here.
    • At this head: 1903 passed, 63 skipped, 1 failed, and 0 forks from engine modules.
    • At the merge-base: 1893 passed, the same 1 failure, and 5 forks, all from presentation_export.py (lines 132, 164, 564, 588 and 660).
    • The one failure is test_dry_run.py::test_complete_dry_evidence_projects_codes_without_values. It fails identically at the base, so it comes from my environment and isn't a regression.
  • I checked the conditions in the CPython 3.12.15 _execute_child source. The helper meets each one: an absolute program path, close_fds=False, and no cwd, start_new_session, preexec_fn or pass_fds. The cwd, start_new_session, preexec_fn and pass_fds options are refused, and no engine module passes cwd= or the other refused options. Every call site passes stdio as None, PIPE or DEVNULL, so the "fd > 2" conditions hold.
  • The new AST guard over openadapt_desktop/ also covers the os.* starters. The Flow CLI call sites (flow_bridge.py:702, 754, 860, 1332) and the portal console (service.py:553) go through the injected runner/popen, whose defaults are now the helper.

Does close_fds=False leak descriptors?

  • Python creates its own fds non-inheritable (PEP 446). No engine module calls dup2, set_inheritable, os.openpty or anything similar.
  • I tried it on Linux. With the engine modules imported, an open sqlite DB, a listening socket, an open file and an os.pipe(), the parent had fds 0-8. A child started through child_process.run had only 0-2, plus the fd ls opens for the listing.
  • Limit: I didn't check fds that native libraries open without CLOEXEC on macOS.

Windows and Linux

  • On Windows, _prepare returns the arguments unchanged and leaves the options alone, so Windows behaviour is identical. That includes taskkill.
  • On Linux the behaviour changes too, harmlessly: the child is started with posix_spawn and no longer forks.

Tests run: test_child_process.py, the portal service, flow bridge, presentation export and approver anchor tests: 201 passed, 7 skipped. Then the full suite above. CI is green (desktop engine tests ubuntu py3.12, build, sidecar, release intent, packages-gate). The Windows and macOS native jobs were skipped on the PR.

P3s

  1. Some options still fall back to fork without any error. process_group, user, group, extra_groups and umask >= 0 aren't refused, and neither is a pipe that lands on fd 0-2 when the engine's own stdio is closed. None of these is used today, so refusing them as well would close the gap.
  2. If a caller passes an env without PATH, a bare program name is looked up on the engine's PATH rather than os.defpath, so its behaviour differs slightly from subprocess. All current callers pass absolute paths or sys.executable.
  3. As the PR says, webbrowser.open and capture's multiprocessing still fork. The latter waits on release(capture): 1.4.0 #2286 plus a Desktop pin raise.

Not run: macOS. The fork-stall fix still needs proving on a macOS 26 run on main, or on a release build, before 0.30.5 ships.

Still to decide (maintainers): merging and the 0.30.5 release.

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