fix: handle proxy cleanup failures during app exit

This commit is contained in:
wonfen
2026-08-19 20:04:21 +08:00
parent fc5fdbfea6
commit 9b9feb9a39
6 changed files with 70 additions and 41 deletions
+18 -5
View File
@@ -170,12 +170,25 @@ where
first_failure([("global proxy", global), ("PAC", pac)])
}
/// Treats a missing network service as a completed disable: with nothing to write to, the
/// requested "no proxy" state already holds. Turning a proxy on must still fail.
fn tolerate_missing_network_service(enabling: bool, outcome: sysproxy::Result<()>) -> Result<()> {
match outcome {
#[cfg(target_os = "macos")]
Err(sysproxy::Error::NoActiveNetworkService) if !enabling => {
logging!(warn, Type::Core, "no active network service, nothing to turn off");
Ok(())
}
other => other.map_err(anyhow::Error::from),
}
}
/// Force both proxy kinds off, in one blocking hop.
async fn disable_all_proxies(sys: Sysproxy, auto: Autoproxy) -> Result<()> {
tokio::task::spawn_blocking(move || {
disable_both(
|| sys.set_system_proxy().map_err(anyhow::Error::from),
|| auto.set_auto_proxy().map_err(anyhow::Error::from),
|| tolerate_missing_network_service(sys.enable, sys.set_system_proxy()),
|| tolerate_missing_network_service(auto.enable, auto.set_auto_proxy()),
)
})
.await?
@@ -472,11 +485,11 @@ impl Sysopt {
let applied = tokio::task::spawn_blocking(move || {
for (index, step) in apply_steps.into_iter().enumerate() {
let written = match step {
ProxyApplyStep::Autoproxy => auto.set_auto_proxy(),
ProxyApplyStep::Sysproxy => sys.set_system_proxy(),
ProxyApplyStep::Autoproxy => tolerate_missing_network_service(auto.enable, auto.set_auto_proxy()),
ProxyApplyStep::Sysproxy => tolerate_missing_network_service(sys.enable, sys.set_system_proxy()),
};
if let Err(error) = written {
return Err((index > 0, anyhow::Error::from(error)));
return Err((index > 0, error));
}
}
Ok(())
+4 -1
View File
@@ -54,7 +54,10 @@ pub async fn restart_app() {
if !cleanup_result.core_stopped {
handle::Handle::global().clear_is_exiting();
handle::Handle::notice_message("app_restart::core_stop_failed", "");
handle::Handle::notice_message(
"app_restart::core_stop_failed",
cleanup_result.stop_error.unwrap_or_default(),
);
return;
}
+14 -7
View File
@@ -4,14 +4,17 @@ use crate::module::lightweight;
use crate::utils;
use crate::utils::window_manager::WindowManager;
use clash_verge_logging::{Type, logging};
use parking_lot::Mutex;
use tokio::time::Duration;
#[cfg(target_os = "macos")]
use tokio::time::timeout;
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Default)]
pub struct CleanupResult {
pub all_success: bool,
pub core_stopped: bool,
/// Why the stop failed, for the cancelled-exit notice.
pub stop_error: Option<String>,
}
const fn should_abort_exit_after_cleanup(core_stopped: bool) -> bool {
@@ -29,14 +32,12 @@ where
AncillaryFuture: std::future::Future<Output = bool>,
{
if !stop_core().await {
return CleanupResult {
all_success: false,
core_stopped: false,
};
return CleanupResult::default();
}
CleanupResult {
all_success: ancillary_cleanup().await,
core_stopped: true,
stop_error: None,
}
}
@@ -128,7 +129,10 @@ pub async fn quit() -> clash_verge_signal::ShutdownOutcome {
if should_abort_exit_after_cleanup(cleanup_result.core_stopped) {
handle::Handle::global().clear_is_exiting();
handle::Handle::notice_message("app_quit::core_stop_failed", "");
handle::Handle::notice_message(
"app_quit::core_stop_failed",
cleanup_result.stop_error.unwrap_or_default(),
);
return clash_verge_signal::ShutdownOutcome::Canceled;
}
@@ -145,7 +149,8 @@ pub async fn clean_async() -> CleanupResult {
"Starting interactive cleanup; controlled core stop will be awaited to completion"
);
let result = run_interactive_cleanup_transition(
let stop_error = Mutex::new(None);
let mut result = run_interactive_cleanup_transition(
|| async {
logging!(info, Type::System, "Stopping core for interactive quit or restart");
match CoreManager::global().stop_core().await {
@@ -159,6 +164,7 @@ pub async fn clean_async() -> CleanupResult {
Type::Window,
"Controlled core stop failed; interactive quit or restart must remain cancelled: {error:#}"
);
*stop_error.lock() = Some(format!("{error:#}"));
false
}
}
@@ -166,6 +172,7 @@ pub async fn clean_async() -> CleanupResult {
restore_dns_after_core_stop,
)
.await;
result.stop_error = stop_error.into_inner();
logging!(
info,