From d25eb49bfefb6b161911f209a635335cb75d40e1 Mon Sep 17 00:00:00 2001 From: Sline Date: Tue, 7 Oct 2025 10:01:15 +0800 Subject: [PATCH] fix(tray): resolve "Restart App" failure on Windows (#4960) * fix(tray): resolve "Restart App" failure on Windows * style: rm useless comment --- src-tauri/src/feat/clash.rs | 67 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src-tauri/src/feat/clash.rs b/src-tauri/src/feat/clash.rs index 5257150e..2cd9584d 100644 --- a/src-tauri/src/feat/clash.rs +++ b/src-tauri/src/feat/clash.rs @@ -26,7 +26,6 @@ pub async fn restart_clash_core() { /// Restart the application pub async fn restart_app() { - // Step 1: Perform cleanup and check for errors if let Err(err) = resolve::resolve_reset_async().await { handle::Handle::notice_message( "restart_app::error", @@ -36,43 +35,41 @@ pub async fn restart_app() { return; } - // Step 2: Attempt to get app handle and restart - match handle::Handle::global().app_handle() { - Some(app_handle) => { - handle::Handle::notice_message("restart_app::info", "Restarting application..."); - app_handle.restart(); + handle::Handle::notice_message("restart_app::info", "Restarting application..."); + + // Use the manual restart method consistently to ensure reliability across platforms + // This addresses the issue where app_handle.restart() doesn't work properly on Windows + let current_exe = match env::current_exe() { + Ok(path) => path, + Err(_) => { + // If we can't get the current executable path, try to use the fallback method + if let Some(app_handle) = handle::Handle::global().app_handle() { + app_handle.restart(); + } + exit(1); // If we reach here, either app_handle was None or restart() failed to restart } - None => { - handle::Handle::notice_message( - "restart_app::error", - "Failed to get app handle for restart", - ); - logging_error!( - Type::System, - false, - "{}", - "Failed to get app handle for restart" - ); + }; - // Fallback: launch a new instance of the application and exit the current one - let current_exe = env::current_exe().unwrap_or_else(|_| { - exit(1); // Exit if can't find the executable path - }); + let mut cmd = Command::new(current_exe); + cmd.args(env::args().skip(1)); - let mut cmd = Command::new(current_exe); - cmd.args(env::args().skip(1)); - - match cmd.spawn() { - Ok(child) => { - log::info!(target: "app", "New application instance started with PID: {}", child.id()); - // Successfully started new process, now exit current process - exit(0); - } - Err(e) => { - log::error!(target: "app", "Failed to start new application instance: {}", e); - // Unable to start new process, exit with error - exit(1); - } + match cmd.spawn() { + Ok(child) => { + log::info!(target: "app", "New application instance started with PID: {}", child.id()); + // Successfully started new process, now exit current process + if let Some(app_handle) = handle::Handle::global().app_handle() { + app_handle.exit(0); + } else { + exit(0); + } + } + Err(e) => { + log::error!(target: "app", "Failed to start new application instance: {}", e); + // If manual spawn fails, try the original restart method as a last resort + if let Some(app_handle) = handle::Handle::global().app_handle() { + app_handle.restart(); + } else { + exit(1); } } }