opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(desktop): add single-instance protection to prevent multiple app spawns

Open usvimal opened this issue 1 week ago • 1 comments

Summary

  • Adds tauri-plugin-single-instance to 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:

  1. Show "Startup Failed" dialog
  2. Call app.exit(1)
  3. 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

  1. Kill sidecar on startup failure: Added child.kill() before showing the failure dialog and exiting
  2. Single-instance guard: Added tauri-plugin-single-instance to prevent concurrent launches (secondary fix)

Changes

  • packages/desktop/src-tauri/Cargo.toml: Added tauri-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

usvimal avatar Jan 05 '26 09:01 usvimal