From a7fa63f0548e182d2a23113d6c64b4a7c658713d Mon Sep 17 00:00:00 2001 From: Sline Date: Tue, 7 Oct 2025 07:32:01 +0800 Subject: [PATCH] fix: implement proper error handling for "Restart App" failures (#4951) * fix: implement proper error handling for "Restart App" failures * fix: make clippy happy --- src-tauri/src/feat/clash.rs | 21 +++++++++++++++++---- src-tauri/src/utils/resolve/mod.rs | 12 +++++------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/feat/clash.rs b/src-tauri/src/feat/clash.rs index 89fdfc78c..5257150e7 100644 --- a/src-tauri/src/feat/clash.rs +++ b/src-tauri/src/feat/clash.rs @@ -26,22 +26,35 @@ pub async fn restart_clash_core() { /// Restart the application pub async fn restart_app() { - // logging_error!(Type::Core, true, CoreManager::global().stop_core().await); - resolve::resolve_reset_async().await; + // Step 1: Perform cleanup and check for errors + if let Err(err) = resolve::resolve_reset_async().await { + handle::Handle::notice_message( + "restart_app::error", + format!("Failed to cleanup resources: {err}"), + ); + log::error!(target:"app", "Restart failed during cleanup: {err}"); + 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(); } 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, attempting alternative restart method" + "Failed to get app handle for restart" ); - // Fallback: launch a new instance of the application and exit the current one + // 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 }); diff --git a/src-tauri/src/utils/resolve/mod.rs b/src-tauri/src/utils/resolve/mod.rs index f0fca765d..7744de265 100644 --- a/src-tauri/src/utils/resolve/mod.rs +++ b/src-tauri/src/utils/resolve/mod.rs @@ -103,16 +103,12 @@ pub fn resolve_setup_async() { } // 其它辅助函数不变 -pub async fn resolve_reset_async() { +pub async fn resolve_reset_async() -> Result<(), anyhow::Error> { logging!(info, Type::Tray, true, "Resetting system proxy"); - logging_error!( - Type::System, - true, - sysopt::Sysopt::global().reset_sysproxy().await - ); + sysopt::Sysopt::global().reset_sysproxy().await?; logging!(info, Type::Core, true, "Stopping core service"); - logging_error!(Type::Core, true, CoreManager::global().stop_core().await); + CoreManager::global().stop_core().await?; #[cfg(target_os = "macos")] { @@ -121,6 +117,8 @@ pub async fn resolve_reset_async() { logging!(info, Type::System, true, "Restoring system DNS settings"); restore_public_dns().await; } + + Ok(()) } pub fn init_handle(app_handle: AppHandle) {