From 6a752994cf5f33c85a21ce3a590ff250b80e9846 Mon Sep 17 00:00:00 2001 From: wonfen Date: Fri, 25 Sep 2026 10:45:33 +0800 Subject: [PATCH] fix(service): fail fast when the Windows service is stopped A registered but stopped service made every version probe back off for about two minutes, blocking startup before the window and core (#8059). Check the SCM first; a service that is starting, or not yet started this boot, still goes through the IPC retries. --- Changelog.md | 1 + src-tauri/src/core/runstate/env.rs | 8 ++++++++ src-tauri/src/core/service.rs | 31 ++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Changelog.md b/Changelog.md index ad3313215..5292c5b26 100644 --- a/Changelog.md +++ b/Changelog.md @@ -9,6 +9,7 @@ - 修复 Windows 部分未安装服务的用户以普通权限运行时,内核无法启动的问题 - 修复 Windows 系统隔离权限被误判,导致服务模式和 TUN 无法使用的问题 - 修复 Windows 旧版服务状态残留导致无法重装的问题,自动备份可识别的残留并恢复安装 +- 修复 Windows 服务已停止时,启动要等约两分钟才显示窗口的问题 - 优化 Windows 服务安全检查失败提示:用易懂的文字说明原因,并提供修复文档链接 **🍎 macOS** diff --git a/src-tauri/src/core/runstate/env.rs b/src-tauri/src/core/runstate/env.rs index fdc1436a6..8d06acbb5 100644 --- a/src-tauri/src/core/runstate/env.rs +++ b/src-tauri/src/core/runstate/env.rs @@ -27,6 +27,14 @@ pub struct RealEnv; impl RunStateEnv for RealEnv { async fn probe_service_version(&self) -> Result { + // The IPC client would otherwise back off for about two minutes before failing. + #[cfg(windows)] + if tokio::task::spawn_blocking(crate::core::service::service_stopped) + .await + .context("service status probe did not finish")?? + { + anyhow::bail!("the Windows service is not running"); + } let response = clash_verge_service_ipc::get_version().await?; let core = if response.code == 0 && response.data.as_ref().is_some_and(|info| { diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index ec98dcd0e..be485e2f2 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -242,7 +242,7 @@ fn macos_service_install_marker_exists() -> std::io::Result { } #[cfg(windows)] -pub(crate) fn trusted_service_evidence() -> Result { +fn open_registered_service() -> Result> { use windows_service::{ Error as WindowsServiceError, service::ServiceAccess, @@ -255,17 +255,36 @@ pub(crate) fn trusted_service_evidence() -> Result { clash_verge_service_ipc::WINDOWS_SERVICE_NAME, ServiceAccess::QUERY_STATUS, ) { - Ok(service) => { - drop(service); - Ok(true) - } + Ok(service) => Ok(Some(service)), Err(WindowsServiceError::Winapi(error)) if error.raw_os_error() == Some(ERROR_SERVICE_DOES_NOT_EXIST) => { - Ok(false) + Ok(None) } Err(error) => Err(error).context("failed to inspect Windows service registration"), } } +#[cfg(windows)] +pub(crate) fn trusted_service_evidence() -> Result { + Ok(open_registered_service()?.is_some()) +} + +/// Whether IPC cannot succeed until the service is started again. A service that is starting, or +/// that the SCM has not started yet this boot, is left to the IPC retries, which wait for it. +#[cfg(windows)] +pub(crate) fn service_stopped() -> Result { + use windows_service::service::{ServiceExitCode, ServiceState}; + + const ERROR_SERVICE_NEVER_STARTED: u32 = 1077; + let Some(service) = open_registered_service()? else { + return Ok(true); + }; + let status = service + .query_status() + .context("failed to query Windows service status")?; + Ok(status.current_state == ServiceState::Stopped + && status.exit_code != ServiceExitCode::Win32(ERROR_SERVICE_NEVER_STARTED)) +} + #[cfg(target_os = "linux")] pub(crate) fn trusted_service_evidence() -> Result { let unit = format!("{}.service", clash_verge_service_ipc::SERVICE_SLUG);