fix: implement proper error handling for "Restart App" failures (#4951)

* fix: implement proper error handling for "Restart App" failures

* fix: make clippy happy
This commit is contained in:
Sline
2025-10-07 07:32:01 +08:00
committed by GitHub
parent b20b30baad
commit a7fa63f054
2 changed files with 22 additions and 11 deletions

View File

@@ -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
});

View File

@@ -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) {