[PR #6922] fix(desktop): prevent multiple app instances with single-instance plugin #12163

Closed
opened 2026-02-16 18:17:05 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6922

State: closed
Merged: No


Summary

  • Adds tauri-plugin-single-instance to prevent multiple instances of OpenCode Desktop from running simultaneously
  • When a second instance is launched, it focuses the existing window instead of spawning a new one

Problem

When launching OpenCode Desktop multiple times (intentionally or accidentally), the app spawns multiple instances. Each instance:

  1. Calls get_sidecar_port() which binds to a random free port (since OPENCODE_PORT is typically not set)
  2. Checks is_server_running(port) which returns false for the new random port
  3. Spawns a new opencode-cli serve --port=XXXXX process

This leads to:

  • Multiple windows opening unexpectedly
  • Multiple background opencode-cli serve processes consuming resources
  • Port exhaustion in extreme cases
  • User confusion

Solution

Uses Tauri's official tauri-plugin-single-instance plugin:

  1. On app launch, the plugin acquires a system-wide lock
  2. If another instance already holds the lock, the new instance sends its arguments to the existing instance and exits
  3. The existing instance receives a callback and focuses its window

This is the standard pattern recommended by Tauri for single-instance apps.

Changes

  • Cargo.toml: Added tauri-plugin-single-instance = "2" dependency
  • lib.rs: Added single-instance plugin initialization with window focus callback

Testing

  • Launch app → works normally
  • Launch app again → existing window focuses, no new instance spawned
  • Close app → lock released, can launch fresh instance
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6922 **State:** closed **Merged:** No --- ## Summary - Adds `tauri-plugin-single-instance` to prevent multiple instances of OpenCode Desktop from running simultaneously - When a second instance is launched, it focuses the existing window instead of spawning a new one ## Problem When launching OpenCode Desktop multiple times (intentionally or accidentally), the app spawns multiple instances. Each instance: 1. Calls `get_sidecar_port()` which binds to a **random free port** (since `OPENCODE_PORT` is typically not set) 2. Checks `is_server_running(port)` which returns `false` for the new random port 3. Spawns a new `opencode-cli serve --port=XXXXX` process This leads to: - Multiple windows opening unexpectedly - Multiple background `opencode-cli serve` processes consuming resources - Port exhaustion in extreme cases - User confusion ## Solution Uses Tauri's official `tauri-plugin-single-instance` plugin: 1. On app launch, the plugin acquires a system-wide lock 2. If another instance already holds the lock, the new instance sends its arguments to the existing instance and exits 3. The existing instance receives a callback and focuses its window This is the standard pattern recommended by Tauri for single-instance apps. ## Changes - `Cargo.toml`: Added `tauri-plugin-single-instance = "2"` dependency - `lib.rs`: Added single-instance plugin initialization with window focus callback ## Testing - Launch app → works normally - Launch app again → existing window focuses, no new instance spawned - Close app → lock released, can launch fresh instance
yindo added the pull-request label 2026-02-16 18:17:05 -05:00
yindo closed this issue 2026-02-16 18:17:05 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12163