From ba6649f441890dec42df002d3cc2464c9dc810ff Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Sun, 6 Dec 2026 08:42:08 +0800 Subject: [PATCH] fix(service): allow maintenance of epoch 2 services after upgrade --- Cargo.lock | 2 +- Cargo.toml | 2 +- Changelog.md | 2 +- src-tauri/src/cmd/service.rs | 8 +-- src-tauri/src/core/manager/lifecycle.rs | 76 +++++++++++++++++++------ src-tauri/src/core/service.rs | 36 ++++++++++++ 6 files changed, 100 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3cf8e6fee..96ed01d97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1343,7 +1343,7 @@ dependencies = [ [[package]] name = "clash_verge_service_ipc" version = "2.8.3" -source = "git+https://github.com/clash-verge-rev/clash-verge-service-ipc.git?rev=fd00f5af2363517e67de57a7ead67737175ac376#fd00f5af2363517e67de57a7ead67737175ac376" +source = "git+https://github.com/clash-verge-rev/clash-verge-service-ipc.git?rev=80ef40701dd7d13510d9bf53ef0b09c527576502#80ef40701dd7d13510d9bf53ef0b09c527576502" dependencies = [ "anyhow", "kode-bridge", diff --git a/Cargo.toml b/Cargo.toml index ca5f8b50b..b99e563a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,7 +74,7 @@ clash-verge-runtime-bundle = { path = "crates/clash-verge-runtime-bundle" } clash-verge-proxy-view = { path = "crates/clash-verge-proxy-view" } clash-verge-profile = { path = "crates/clash-verge-profile" } clash-verge-backup-archive = { path = "crates/clash-verge-backup-archive" } -clash_verge_service_ipc = { git = "https://github.com/clash-verge-rev/clash-verge-service-ipc.git", rev = "fd00f5af2363517e67de57a7ead67737175ac376", version = "2.8.3", default-features = false } +clash_verge_service_ipc = { git = "https://github.com/clash-verge-rev/clash-verge-service-ipc.git", rev = "80ef40701dd7d13510d9bf53ef0b09c527576502", version = "2.8.3", default-features = false } tracing-estuary = { git = "https://github.com/Tunglies/tracing-estuary.git", branch = "main" } tauri-plugin-clash-verge-sysinfo = { path = "crates/tauri-plugin-clash-verge-sysinfo" } tracing = "0.1.44" diff --git a/Changelog.md b/Changelog.md index 864062a63..9f6c0d571 100644 --- a/Changelog.md +++ b/Changelog.md @@ -23,7 +23,7 @@ GitHub, so they never reach users. --> ## v2.6.0 -- 使用 2.6.x 服务模式时请安装随附的服务,2.5.x 客户端无法使用新版服务 +- 升级至 2.6.x 后,可在应用内重装或卸载旧版服务;使用服务模式需安装随附的服务 - **Mihomo 内核升级至 v1.19.34**
diff --git a/src-tauri/src/cmd/service.rs b/src-tauri/src/cmd/service.rs index ed424d70f..44f593af4 100644 --- a/src-tauri/src/cmd/service.rs +++ b/src-tauri/src/cmd/service.rs @@ -16,12 +16,10 @@ async fn execute_service_operation_sync(status: ServiceStatus, error_code: &str) &status, ServiceStatus::ReinstallRequired | ServiceStatus::ForceReinstallRequired ) { - manager - .controlled_stop_core_inner() - .await - .map_err(|error| proxy_aware_coded_error(&error, error_code))?; + manager.maintain_service(status).await + } else { + SERVICE_MANAGER.handle_service_status(status).await } - SERVICE_MANAGER.handle_service_status(status).await }; result.map_err(|error| proxy_aware_coded_error(&error, error_code)) } diff --git a/src-tauri/src/core/manager/lifecycle.rs b/src-tauri/src/core/manager/lifecycle.rs index af38e506f..6db76218b 100644 --- a/src-tauri/src/core/manager/lifecycle.rs +++ b/src-tauri/src/core/manager/lifecycle.rs @@ -266,11 +266,16 @@ where restore_proxy().await } +enum UninstallFailure { + Maintenance(anyhow::Error), + Sidecar(anyhow::Error), +} + async fn run_uninstall_transition( uninstall: Uninstall, prepare_sidecar: Prepare, start_sidecar: Start, -) -> Result<()> +) -> Result<(), UninstallFailure> where Uninstall: FnOnce() -> UninstallFuture, UninstallFuture: std::future::Future>, @@ -279,9 +284,9 @@ where Start: FnOnce() -> StartFuture, StartFuture: std::future::Future>, { - uninstall().await?; - prepare_sidecar().await?; - start_sidecar().await + uninstall().await.map_err(UninstallFailure::Maintenance)?; + prepare_sidecar().await.map_err(UninstallFailure::Sidecar)?; + start_sidecar().await.map_err(UninstallFailure::Sidecar) } fn report_sidecar_failure(error: anyhow::Error) -> anyhow::Error { @@ -412,15 +417,8 @@ impl CoreManager { let _config_write = Config::lock_config_write().await; let _life = self.lifecycle_lock.lock().await; - self.controlled_stop_core_inner().await?; - crate::core::service::release_service_core_for_sidecar().await?; - let result = run_uninstall_transition( - || async { - SERVICE_MANAGER - .handle_service_status(ServiceStatus::UninstallRequired) - .await - }, + || self.maintain_service(ServiceStatus::UninstallRequired), || async { // Must be asked after the uninstall: until then the Service still makes TUN capable. let tun_enabled = Config::verge().await.latest_arc().enable_tun_mode.unwrap_or(false); @@ -446,12 +444,49 @@ impl CoreManager { ) .await; - if let Err(error) = result { - if let Err(cleanup_error) = self.rollback_failed_sidecar_transition().await { - return Err(proxy_control::rollback_failure(error, cleanup_error) - .context("failed to clear the proxy before rolling back")); + match result { + Ok(()) => Ok(()), + Err(UninstallFailure::Maintenance(error)) => Err(error), + Err(UninstallFailure::Sidecar(error)) => { + let error = match self.rollback_failed_sidecar_transition().await { + Ok(()) => error, + Err(cleanup) => proxy_control::rollback_failure(error, cleanup), + }; + let error = report_sidecar_failure(error.context("Service uninstalled; Sidecar startup failed")); + logging!(warn, Type::Core, "{error:#}"); + Ok(()) + } + } + } + + /// The caller holds the lifecycle lock across both preparation and the privileged operation. + pub(crate) async fn maintain_service(&self, status: ServiceStatus) -> Result<()> { + let mode = *self.get_running_mode(); + let uninstall = matches!(status, ServiceStatus::UninstallRequired); + let legacy = (uninstall || mode == RunningMode::Service) && crate::core::service::has_epoch_two_service().await; + if legacy && mode == RunningMode::Service { + // The helper stops the old daemon and checks its child under the service owner lock. + #[cfg(target_os = "macos")] + warn_if_guard_lingers(proxy_control::stop_guard().await); + #[cfg(not(target_os = "macos"))] + proxy_control::clear().await?; + } else { + self.controlled_stop_core_inner().await?; + } + if uninstall && !legacy { + crate::core::service::release_service_core_for_sidecar().await?; + } + SERVICE_MANAGER.handle_service_status(status).await?; + if legacy { + crate::core::service::discard_replaced_service_session(); + if mode == RunningMode::Service { + // A repaired daemon can restore its core; this GUI must acquire a fresh session. + self.core_stopped(); + #[cfg(target_os = "macos")] + if uninstall { + crate::utils::resolve::dns::sync_public_dns().await; + } } - return Err(error); } Ok(()) } @@ -834,7 +869,12 @@ mod tests { .and_then(|failed| steps.iter().position(|step| *step == failed).map(|index| index + 1)) .unwrap_or(steps.len()); assert_eq!(&*calls.lock(), &steps[..expected_len]); - assert_eq!(result.is_err(), fail_at.is_some()); + match (fail_at, result) { + (None, Ok(())) + | (Some("uninstall"), Err(super::UninstallFailure::Maintenance(_))) + | (Some("prepare-sidecar" | "start-sidecar"), Err(super::UninstallFailure::Sidecar(_))) => {} + _ => panic!("wrong uninstall outcome for {fail_at:?}"), + } } } diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index ea212127d..08acd1f65 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -632,6 +632,27 @@ fn record_service_start_refusal( refusal.into() } +pub(super) async fn has_epoch_two_service() -> bool { + let Ok(Ok(response)) = tokio::time::timeout(Duration::from_secs(2), clash_verge_service_ipc::get_version()).await + else { + return false; + }; + is_epoch_two_service(response.code, response.data.as_ref()) +} + +fn is_epoch_two_service(code: u16, info: Option<&clash_verge_service_ipc::ProtocolInfo>) -> bool { + code == 0 + && info.is_some_and(|info| { + info.protocol.epoch == 2 && info.protocol.revision > 0 && !info.build_version.is_empty() + }) +} + +pub(super) fn discard_replaced_service_session() { + cancel_owner_monitors(); + PENDING_SERVICE_START.lock().take(); + clear_active_service_session(); +} + pub(super) fn require_service_core_released() -> Result<()> { anyhow::ensure!( PENDING_SERVICE_START.lock().is_none() && ACTIVE_SERVICE_SESSION.lock().is_none(), @@ -1992,6 +2013,21 @@ mod tests { ServiceStatus::from_run_state(&store.settled().await) } + #[test] + fn maintenance_requires_an_affirmative_old_version_reply() { + use clash_verge_service_ipc::ProtocolInfo; + + let mut info = ProtocolInfo::current(); + assert!(!super::is_epoch_two_service(0, Some(&info))); + assert!(!super::is_epoch_two_service(0, None)); + info.protocol.epoch = 2; + info.protocol.revision = 6; + assert!(super::is_epoch_two_service(0, Some(&info))); + assert!(!super::is_epoch_two_service(1007, Some(&info))); + info.protocol.epoch = 1; + assert!(!super::is_epoch_two_service(0, Some(&info))); + } + #[test] fn an_idle_snapshot_releases_an_ambiguous_start_only_after_repair() { let mut pending = super::PendingServiceStart::new("lost-reply".into());