Repository navigation
fix(ai): stop leaking opencode serve processes - #1445
Conversation
Every server boot with the opencode CLI on PATH eagerly called the provider's fetchModels() to fill the Ask AI dropdown, which spawned (or attached to) an 'opencode serve' on the shared default port 4096. Dispose only ran on the clean decision path, so Ctrl-C orphaned the child, and every later session attached to the orphan and piled unevictable per-directory instances into it (multi-GB over a day of normal use). Three changes: - Lazy start: opencode model discovery moves onto the same deferred provider initializer Codex uses. Nothing spawns until the user activates opencode in Ask AI (?activate= from the model picker, or the first opencode session). The picker still lists the provider with an empty model list pre-activation, exactly like Codex. - Own server per process: spawn with port 0 (OS-assigned; the SDK reads the real URL from the child's listening line) and never attach to a server we did not spawn. An explicitly configured port is honored. - Exit cleanup: a process 'exit' handler closes the spawned server (SIGINT/SIGTERM are routed through process.exit by the CLI), removed again on dispose. No SIGHUP listener, preserving nohup. Both runtimes; regression tests mock the SDK so no real server spawns.
Independent review of the leak fix found two holes, both now closed and regression-tested against the mocked SDK: - A failure after the spawn (client construction) left the child running and its exit handler registered, and because the handler read this.server late instead of capturing its own server, a retry's second spawn made the first unreachable by any cleanup. doStart now captures the server in its handler closure and reaps child + handler on any post-spawn failure. - dispose() during an in-flight spawn was a no-op the completing spawn then undid, resurrecting a disposed provider with a live child and a fresh exit handler. dispose() now bumps a start epoch; a spawn that completes past its epoch reaps its own server and rejects, and the provider remains restartable afterwards. Also documents the OpenCode transport (per-process server, deferred discovery) beside the Codex note in AGENTS.md.
|
Independent adversarial review ran against this branch (verdict: merge with minor changes) and empirically confirmed the fix end to end: The review found two lifecycle holes, closed in 698d97b with tests:
Known residual, by design: SIGHUP still orphans the child, because the CLI deliberately installs no SIGHUP listener ( AI-assisted (Claude) under maintainer direction. |
The leak
Anyone with the
opencodeCLI on PATH was affected on every Plannotator session, on both runtimes. Server boot eagerly called the opencode provider'sfetchModels()just to fill the Ask AI model dropdown, which spawned anopencode serve(or attached to an existing one) on the shared default port 4096. Cleanup only ran on the clean decision path, so a Ctrl-C, crash, or host-killed hook orphaned the child. From there it compounded: the orphan owned 4096, every later session attached to it, and each attach from a new directory created a ~130MB instance inside it that opencode never evicts. Observed in the wild as a 2.7GBopencode servenobody started on purpose.The fix
Lazy start (the core). The autolaunch itself was the problem: spawning a whole agent server at boot to populate a dropdown most users never open. opencode model discovery now sits behind the same deferred provider initializer Codex uses (#1144 pattern): nothing spawns until the user activates opencode in Ask AI (
?activate=from the model picker viauseAIProviderActivation, or the first opencode session). Pre-activation the picker lists the provider with an empty model list, exactly like Codex — bothendpoints.ts(models: p.models ?? []) and the UI (provider.models ?? []) already handle that state.Own server per process. When it does start, it spawns with
port: 0— the SDK parses the real URL from the child's own "listening" line, so the OS assigns a free port — and the attach path is removed entirely: we never connect to a server we didn't spawn, because we can't clean up what we don't own and its instance cache grows without bound. An explicitly configured port is still honored verbatim.Exit cleanup. A
process.once("exit")handler closes the spawned server; the CLI already routes SIGINT/SIGTERM throughprocess.exit, so Ctrl-C now kills the child. Dispose removes the handler. No SIGHUP listener is installed, preserving thenohupcontract.Both runtimes
packages/server/ai-runtime.tsandapps/pi-extension/server/ai-runtime.tsget the same deferral; the provider itself is shared (vendored to Pi byvendor.sh).Tests
packages/ai/providers/opencode-sdk.test.ts(5 tests): SDK mocked so no real server ever spawns — asserts port 0, exit-handler add/remove symmetry, the exit handler actually closing the server, explicit-port passthrough, and a source-level pin that BOTH runtimes keep opencode discovery behindproviderInitializersinstead of eagermodelDiscovery.bun run typecheckclean across all 9 projects.bun test packages/ai/127 pass. The 6 failures inpackages/server/review-workspace.test.tsand 6 inapps/pi-extension/server.test.tsreproduce identically on clean main (sem-sidecar/GitButler environment) and are unrelated.AI-assisted (Claude) under maintainer direction.