fix(service): allow maintenance of epoch 2 services after upgrade

This commit is contained in:
Tunglies
2026-12-06 08:42:08 +08:00
parent 74b815db59
commit ba6649f441
6 changed files with 100 additions and 26 deletions
Generated
+1 -1
View File
@@ -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",
+1 -1
View File
@@ -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"
+1 -1
View File
@@ -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**
<details>
+3 -5
View File
@@ -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))
}
+58 -18
View File
@@ -266,11 +266,16 @@ where
restore_proxy().await
}
enum UninstallFailure {
Maintenance(anyhow::Error),
Sidecar(anyhow::Error),
}
async fn run_uninstall_transition<Uninstall, UninstallFuture, Prepare, PrepareFuture, Start, StartFuture>(
uninstall: Uninstall,
prepare_sidecar: Prepare,
start_sidecar: Start,
) -> Result<()>
) -> Result<(), UninstallFailure>
where
Uninstall: FnOnce() -> UninstallFuture,
UninstallFuture: std::future::Future<Output = Result<()>>,
@@ -279,9 +284,9 @@ where
Start: FnOnce() -> StartFuture,
StartFuture: std::future::Future<Output = Result<()>>,
{
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:?}"),
}
}
}
+36
View File
@@ -632,6 +632,27 @@ fn record_service_start_refusal<E: RunStateEnv>(
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());