From 4779d99a1361f689f432a218f61a7a27a57c6c36 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Tue, 16 Dec 2025 19:01:28 +0800 Subject: [PATCH] tauri: explicitly kill sidecar before updater relaunch --- packages/tauri/src-tauri/src/lib.rs | 102 +++++++--------------------- packages/tauri/src/updater.ts | 2 + 2 files changed, 27 insertions(+), 77 deletions(-) diff --git a/packages/tauri/src-tauri/src/lib.rs b/packages/tauri/src-tauri/src/lib.rs index aab2ce5f3..02730281f 100644 --- a/packages/tauri/src-tauri/src/lib.rs +++ b/packages/tauri/src-tauri/src/lib.rs @@ -1,13 +1,11 @@ use std::{ net::{SocketAddr, TcpListener}, - process::Command, sync::{Arc, Mutex}, time::{Duration, Instant}, }; #[cfg(target_os = "macos")] use tauri::TitleBarStyle; -use tauri::{AppHandle, LogicalSize, Manager, Monitor, RunEvent, WebviewUrl, WebviewWindow}; -use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogResult}; +use tauri::{AppHandle, LogicalSize, Manager, RunEvent, WebviewUrl, WebviewWindow}; use tauri_plugin_shell::process::{CommandChild, CommandEvent}; use tauri_plugin_shell::ShellExt; use tokio::net::TcpSocket; @@ -15,6 +13,28 @@ use tokio::net::TcpSocket; #[derive(Clone)] struct ServerState(Arc>>); +#[tauri::command] +fn kill_sidecar(app: AppHandle) { + let Some(server_state) = app.try_state::() else { + println!("Server not running"); + return; + }; + + let Some(server_state) = server_state + .0 + .lock() + .expect("Failed to acquire mutex lock") + .take() + else { + println!("Server state missing"); + return; + }; + + let _ = server_state.kill(); + + println!("Killed server"); +} + fn get_sidecar_port() -> u16 { option_env!("OPENCODE_PORT") .map(|s| s.to_string()) @@ -29,40 +49,6 @@ fn get_sidecar_port() -> u16 { }) } -fn find_and_kill_process_on_port(port: u16) -> Result<(), Box> { - // Find all listeners on the specified port - let listeners = listeners::get_processes_by_port(port)?; - - if listeners.is_empty() { - println!("No processes found listening on port {}", port); - return Ok(()); - } - - for listener in listeners { - let pid = listener.pid; - println!("Found process {} listening on port {}", pid, port); - - // Kill the process using platform-appropriate command - #[cfg(target_os = "windows")] - { - Command::new("taskkill") - .args(["/F", "/PID", &pid.to_string()]) - .output()?; - } - - #[cfg(not(target_os = "windows"))] - { - Command::new("kill") - .args(["-9", &pid.to_string()]) - .output()?; - } - - println!("Killed process {}", pid); - } - - Ok(()) -} - fn spawn_sidecar(app: &AppHandle, port: u16) -> CommandChild { let (mut rx, child) = app .shell() @@ -116,6 +102,7 @@ pub fn run() { .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_opener::init()) + .invoke_handler(tauri::generate_handler![kill_sidecar]) .setup(move |app| { let app = app.handle().clone(); @@ -124,28 +111,6 @@ pub fn run() { let should_spawn_sidecar = !is_server_running(port).await; - // if server_running { - // let res = app - // .dialog() - // .message( - // "OpenCode Server is already running, would you like to restart it?", - // ) - // .buttons(MessageDialogButtons::YesNo) - // .blocking_show_with_result(); - - // match res { - // MessageDialogResult::Yes => { - // if let Err(e) = find_and_kill_process_on_port(port) { - // eprintln!("Failed to kill process on port {}: {}", port, e); - // } - // true - // } - // _ => false, - // } - // } else { - // true - // }; - let child = if should_spawn_sidecar { let child = spawn_sidecar(&app, port); @@ -218,24 +183,7 @@ pub fn run() { if let RunEvent::Exit = event { println!("Received Exit"); - let Some(server_state) = app.try_state::() else { - println!("Server not running"); - return; - }; - - let Some(server_state) = server_state - .0 - .lock() - .expect("Failed to acquire mutex lock") - .take() - else { - println!("Server state missing"); - return; - }; - - let _ = server_state.kill(); - - println!("Killed server"); + kill_sidecar(app.clone()); } }); } diff --git a/packages/tauri/src/updater.ts b/packages/tauri/src/updater.ts index d62aae424..2dd94e831 100644 --- a/packages/tauri/src/updater.ts +++ b/packages/tauri/src/updater.ts @@ -1,6 +1,7 @@ import { check, DownloadEvent } from "@tauri-apps/plugin-updater" import { relaunch } from "@tauri-apps/plugin-process" import { ask, message } from "@tauri-apps/plugin-dialog" +import { invoke } from "@tauri-apps/api/core" export const UPDATER_ENABLED = window.__OPENCODE__?.updaterEnabled ?? false @@ -34,6 +35,7 @@ export async function runUpdater(onDownloadEvent?: (progress: DownloadEvent) => return false } + await invoke("kill_sidecar") await relaunch() return true