fix(tray): resolve "Restart App" failure on Windows (#4960)

* fix(tray): resolve "Restart App" failure on Windows

* style: rm useless comment
This commit is contained in:
Sline
2025-10-07 10:01:15 +08:00
committed by GitHub
parent f3f8ea0481
commit d25eb49bfe

View File

@@ -26,7 +26,6 @@ pub async fn restart_clash_core() {
/// Restart the application /// Restart the application
pub async fn restart_app() { pub async fn restart_app() {
// Step 1: Perform cleanup and check for errors
if let Err(err) = resolve::resolve_reset_async().await { if let Err(err) = resolve::resolve_reset_async().await {
handle::Handle::notice_message( handle::Handle::notice_message(
"restart_app::error", "restart_app::error",
@@ -36,28 +35,20 @@ pub async fn restart_app() {
return; 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..."); 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(); app_handle.restart();
} }
None => { exit(1); // If we reach here, either app_handle was None or restart() failed to restart
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); let mut cmd = Command::new(current_exe);
cmd.args(env::args().skip(1)); cmd.args(env::args().skip(1));
@@ -66,17 +57,23 @@ pub async fn restart_app() {
Ok(child) => { Ok(child) => {
log::info!(target: "app", "New application instance started with PID: {}", child.id()); log::info!(target: "app", "New application instance started with PID: {}", child.id());
// Successfully started new process, now exit current process // 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); exit(0);
} }
}
Err(e) => { Err(e) => {
log::error!(target: "app", "Failed to start new application instance: {}", e); log::error!(target: "app", "Failed to start new application instance: {}", e);
// Unable to start new process, exit with error // 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); exit(1);
} }
} }
} }
} }
}
fn after_change_clash_mode() { fn after_change_clash_mode() {
AsyncHandler::spawn(move || async { AsyncHandler::spawn(move || async {