What Changed#
Fixes cleanup for scoped local OpenCode server processes used during provider refresh/model checks.
The refresh scope now terminates the spawned OpenCode process group when the scoped server is closed, preventing orphaned opencode serve processes from accumulating in the background.
Why#
OpenCode refresh/model checks could start a temporary local server, but the wrapper process could exit while leaving the actual long-lived serve child alive. Since refresh runs periodically, this could create a new background OpenCode process every cycle and cause memory usage to balloon.
This keeps provider refresh behavior intact and only tightens cleanup for the local refresh-owned OpenCode process. Active OpenCode thread sessions keep their existing lifecycle.
Warning#
I couldn't test on windows and the exit logic is slightly different as process.kill(-pid, signal) is POSIX only. It's correct in theory but should be double checked.
Checklist#
Constraint: Keep active OpenCode thread sessions scoped to their own session lifecycle.
Confidence: high
Scope-risk: narrow
Tested: bun run test src/provider/Layers/OpenCodeProvider.test.ts src/provider/Layers/OpenCodeAdapter.test.ts
Tested: bun fmt
Tested: bun lint
Tested: bun typecheck
Tested: bun run build:desktop
Screenshots#
After leaving my laptop overnight

Every 60s a new opencode process is launched and never exits.

Note
Medium Risk
Changes process-spawning/termination behavior for the local opencode serve used during provider refresh, including POSIX process-group killing and Windows-specific fallback; mistakes could leave servers running or kill the wrong process. Scope is narrow and covered by a new refresh cleanup test.
Overview
Fixes a refresh-time memory leak by ensuring locally spawned opencode serve processes are reliably cleaned up when the refresh scope closes.
startOpenCodeServerProcess now spawns the server detached on non-Windows and registers a scope finalizer that SIGTERM/SIGKILLs the entire process group (or uses child.kill on Windows), preventing orphaned background servers from accumulating.
Tests update the OpenCodeRuntime test double to track scope finalization and add an assertion that provider.refresh closes the local server scope.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit f446782d664e373a282fcb54369b6304dfc76e25. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).
Note
Fix memory leak by terminating OpenCode server processes on scope finalization#
- Spawns the OpenCode server process with
detached: true on non-Windows so the child and its subprocesses form a separate process group.
- Registers a scope finalizer that sends SIGTERM, waits 1 second, then SIGKILL to the process group (non-Windows) or calls
child.kill (Windows), ensuring server processes are cleaned up when provider.refresh completes.
- Adds a
closeCalls counter to the test double and a new test asserting the local server scope is closed after refresh.
- Behavioral Change: OpenCode server processes are now spawned detached on non-Windows and will be forcibly killed on scope close rather than left running.
Macroscope summarized f446782.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
53fbc363-cf98-401c-b361-99374ae5c8c9You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
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.
❤️ Share