Repository navigation
Conversation
…down path The macOS helper is spawned detached and its ChildProcess handle never left startMacOSNativeProviderSocket(), so the only way to stop a running helper was a `terminate` socket message — which a wedged helper never reads. Every path that abandoned a socket left the process alive, and the next request spawned a replacement, growing without bound (stablyai#9141: ~200 helpers in 3h, 40-50GB). Thread the handle back to an owner adopted at spawn time and reap it (SIGTERM, then SIGKILL after a grace period) on every teardown path. Signals target the ChildProcess handle rather than a pid so a recycled pid can never be hit, and a WeakSet makes reaping exactly-once across overlapping paths.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThe macOS provider client now owns helper-process lifecycle through Merge Risk: ⚪ Minimal · up to This localized macOS process-lifecycle change is merge-ready after normal checks and review, with no actionable merge-blocking risk remaining. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…0926) Reclaim detached macOS computer-use helpers on abandoned requests and transports. Keep ownership from spawn, escalate SIGTERM to SIGKILL, force pending reaps when the sidecar exits, and clean each failed startup's private socket directory. Based on #14494 by @JuuuuHong. Preserve the original helper ownership/reaping design and regression tests while retaining the upstream line-buffer optimization and adding real-process teardown and resource-bound tests. Fixes #9141. Co-authored-by: JuuuuHong <[email protected]>
|
Merged through #20926 (07c7606), which ports this implementation and regression coverage to current main. @JuuuuHong is credited for the original diagnosis and implementation in the PR description and the squash commit’s Co-authored-by trailer. The merged version retains the newer upstream line buffer and adds synchronous cleanup of pending reaps when the sidecar exits, failed-startup directory cleanup, and real-process regressions. All CI checks passed. Closing this PR as superseded by the merged replacement. Thank you for the original work. |
…ablyai#20926) Reclaim detached macOS computer-use helpers on abandoned requests and transports. Keep ownership from spawn, escalate SIGTERM to SIGKILL, force pending reaps when the sidecar exits, and clean each failed startup's private socket directory. Based on stablyai#14494 by @JuuuuHong. Preserve the original helper ownership/reaping design and regression tests while retaining the upstream line-buffer optimization and adding real-process teardown and resource-bound tests. Fixes stablyai#9141. Co-authored-by: JuuuuHong <[email protected]>
Fixes #9141.
Summary
On macOS,
computer-useleaks helper processes until the machine runs out of memory. #9141 reports ~200orca-computer-use-macosprocesses in 3 hours and 40-50 GB of memory, ending in the "system has run out of application memory" dialog and forced restarts.I hit this on my own machine on 2026-07-16, a day before #9141 was filed — free memory down to ~80 MB, no new app would launch, and dozens of
orca-computer-use-macosprocesses sitting in stateT.After this change every abandoned helper is reclaimed, so the process count stays flat instead of growing with each request. #9141 asks for helpers to be pooled or registered with RunningBoard/jetsam; this takes a third option — reclaiming abandoned helpers at the source — which removes the unbounded growth without changing how helpers are spawned or authorized.
Root cause. The helper is spawned
detachedand no handle escapes the spawn function:startMacOSNativeProviderSocket()returned only socket fields, so the sole way to stop a running helper was a socket message — andterminateonly lands if the helper is still reading its socket, which a wedged helper by definition is not. Every path that abandoned a socket (request timeout, socket close, transport error, write failure, app quit) dropped it and left the process running;detached: truemeans it also survives Orca quitting. The next request then spawns a fresh helper with no reclamation of the previous generation. That is the unbounded growth, and it is consistent with #9141's observed rate of roughly one new helper per 54 seconds against the 60sREQUEST_TIMEOUT_MS.The pre-existing
catchin the transport already didprovider.kill('SIGTERM'), so startup failures — including superseded startups, whosethrowlands in that samecatch— were handled. The gap was every path after a successful connect.Fix. New
macos-native-provider-process-reaping.ts:reapMacOSProviderProcess(provider)— SIGTERM, then SIGKILL after a 2s grace if the process has not exited.ChildProcesshandle, not a raw pid. Node no-ops once the child has exited, so a recycled pid can never be signalled.WeakSetmakes reaping exactly-once per process, so overlapping teardown paths cannot stack duplicate signals or timers.MacOSProviderProcessOwnerholds the current generation;adopt()reaps the outgoing one, so no generation can be dropped even if its teardown path did not run.Wiring:
spawn, not after connect. Adopting on connect left a window up toHELPER_CONNECT_TIMEOUT_MS(10s) where nothing held the handle: a quit during startup reaped nothing and the connect continuation never ran, stranding the helper.cleanupSocketDirectory()becamereleaseHelperGeneration(), reaping the process alongside the socket directory. All four client-side abandon paths already called it, so all four now reclaim the process.handleTransportErrorandinvalidateActiveSocketAfterWriteFailurehad identical bodies apart from the error they reject with, so they are merged intoinvalidateActiveSocket; the file ends up three lines shorter. This also keeps it under the 300-linemax-linescap the added wiring would otherwise have crossed. Happy to split the merge into its own PR if you'd rather review it separately.Four files under
src/main/computer/, +249 / −30.Screenshots
No visual change.
Testing
pnpm lintpnpm typecheckpnpm testpnpm buildSix regressions in
macos-native-provider-client.test.ts:terminategets SIGTERM, then SIGKILL after the grace period.closewith no precedingerror— is reaped. This is the path most likely to run in the wild.With the reaping call stubbed to a no-op, five of the six fail, along with three existing tests covering the transport's
catchbranch. Case 5 asserts that no signal is sent, so it holds either way by construction — it pins the recycled-pid guard rather than the reaping.src/main/computeris 16 files / 97 tests, all passing. On the full suite I see 52,133 pass and 6 fail, none undersrc/main/computer. The same 6 fail on a cleanupstream/maincheckout, so they are not from this change, and they look environment-specific here: 1 inautomation-schedulesis my non-English locale ('일요일s'vs'Sundays'; passes underLC_ALL=en_US.UTF-8), 4 incodex-fetcher-pty-settlepick up real Codex state on this machine instead ofsession: null, and 1 is the#13767shell-ready repro. I'd expect all 6 green in CI — happy to re-run if CI disagrees.The reclamation logic is covered directly by the six tests above. What I could not do is reproduce the
T-state wedge on demand, since a dev-built helper gets a different code identity and therefore a fresh TCC row.AI Review Report
Reviewed with an AI coding agent across three passes — general correctness, process lifetime and concurrency, and test adequacy — with each finding checked by hand against the diff before it was applied.
What it flagged and what changed as a result:
adopt()to immediately afterspawn.adopt()overwrote the previous handle without reaping it, so a teardown path that did not run meant a lost helper.adopt()now reaps the outgoing generation.throwis caught by the same function'scatch, which already reaps — two SIGTERMs, two timers, two listeners. Removed, and aWeakSetnow makes reaping exactly-once regardless of overlapping paths.handleSocketCloseon the active socket — a helper dying and taking its socket down with a bareclose— had no coverage. Added as test 4, plus tests 5 and 6.typeofchecks inhasProviderExited()with!== nullwas proposed for idiomatic style; it would read the test doubles'undefinedas "already exited" and silently skip reaping, so thetypeofform was kept and the reason recorded in a comment.Cross-platform compatibility was explicitly checked for macOS, Linux, and Windows. Every changed file is under the
darwin-gated native-provider path; Linux and Windows go throughDesktopScriptProviderClient, and provider selection incomputer-provider-lifecycle.tsis unchanged. No keyboard shortcuts, accelerators, or user-facing labels are touched, so there are noCmdOrCtrlor⌘/Ctrl+concerns. Paths are built withpath.joinvia the existingmacos-native-provider-pathshelpers; none were added or changed. No shell is invoked —spawnis called with an argv array and noshell: true. The Electron-specific surface touched is process teardown ordering in the computer-use sidecar, which exists only on the macOS native path.Security Audit
spawncall is unchanged, still argv-array form with noshell: true, so no argument-injection surface is added. The change only sends signals to a process this code already owned.ChildProcesshandle rather than a numeric pid. Node treats the handle as inert once the child has exited, which removes the recycled-pid hazard thatprocess.kill(pid)would introduce — this is a reduction in risk, not an addition. Process-group signalling (kill(-pid)) was deliberately not used, since it would reopen that hazard.chmod 0o700, and cleanup are untouched apart from being called from a renamed method.0o600token file, and its removal after connect are unchanged. No token is logged, and no new value is written to disk.REQUIRED_MACOS_PROVIDER_PROTOCOL_VERSION, the wire format, capability negotiation, or peer authorization.isCurrentguard on the transport'scatchcan leaveprovider.tokenintmpdir()when a startup is superseded. It is a0o600file inside a0o700directory, so exposure is limited, but it should be cleaned up — noted below rather than fixed here to keep this PR single-topic.Notes
Platform-specific behavior. macOS only. Windows and Linux are untouched.
Behavior changes worth flagging:
terminatehandling.shutdown()writesterminate, then signals synchronously; the write only reaches the kernel buffer, so the signal typically wins. This is deliberate:sidecar-entry.tscallsprocess.exit(0)immediately aftershutdownProviders()on the SIGTERM path, so a deferred grace period would never execute there. Theterminatewrite is kept ahead of the signal so a helper that reads it promptly still exits on its own terms.signal()/sigaction/DispatchSourceSignalanywhere innative/computer-use-macos/Sources/. Happy to add a note on the Swift side so a future handler doesn't silently regress this.beforeExitpath the SIGKILL escalation cannot fire, since the timer isunref()d and the loop is already draining. That path relies on SIGTERM alone, which is sufficient given the point above.Out of scope, happy to file separately: a cap on concurrent helpers, an orphan sweep at startup, process-group teardown, and the
provider.tokencleanup noted above.Relation to prior work. #11425 and #11441 covered this gap; #11481 rolled them back with the wider process-boundary effort. This is not a re-land of that effort. No supervisor protocol, no main-process ownership, no peer-PID authorization, and no change to the Swift helper. Helper ownership stays in the computer-use sidecar where it is today, and the client-side change extends
cleanupSocketDirectory()— a function every teardown path already called.Possible lead on the wedge itself, not part of this PR: the helper shells out with unbounded waits in two places —
openBundle()runs/usr/bin/open -b <bundleId>andprocessField()runs/bin/ps, both withwaitUntilExit()and no deadline (main.swift:1189,main.swift:3813). A LaunchServices stall there would wedge the helper exactly the way #9141 describes. I can open a separate issue for it.#9141 is assigned to @OrcaWin. If that work is already underway I'm happy to hand this over — otherwise the issue has had no activity since it was filed and this is contained enough to land on its own.