opencode
opencode copied to clipboard
fix(desktop): add single-instance protection to prevent multiple app spawns
Summary
- Adds
tauri-plugin-single-instanceto prevent multiple desktop app instances - Fixes orphaned sidecar cleanup - kills the sidecar process on startup timeout before exiting
Problem
Opening the OpenCode desktop app would spawn orphaned opencode-cli serve processes that accumulate over time, even from a single launch attempt. Users reported 100+ orphaned processes consuming system resources.
Root Cause
When the sidecar fails to start within 7 seconds (e.g., due to module loading errors like the @tarquinen/opencode-dcp Bun compatibility issue), the app would:
- Show "Startup Failed" dialog
- Call
app.exit(1) - But never kill the spawned sidecar process - leaving it orphaned
Each failed launch attempt would leave another orphaned process. The random port allocation (TcpListener::bind("127.0.0.1:0")) meant each orphan ran on a different port.
Solution
-
Kill sidecar on startup failure: Added
child.kill()before showing the failure dialog and exiting -
Single-instance guard: Added
tauri-plugin-single-instanceto prevent concurrent launches (secondary fix)
Changes
-
packages/desktop/src-tauri/Cargo.toml: Addedtauri-plugin-single-instance = "2" -
packages/desktop/src-tauri/src/lib.rs:- Initialize single-instance plugin with window focus callback
- Kill sidecar process on startup timeout before exiting