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.
This commit is contained in:
wonfen
2026-09-25 11:55:01 +08:00
parent 32e5d5bda3
commit 6a752994cf
3 changed files with 34 additions and 6 deletions
+8
View File
@@ -27,6 +27,14 @@ pub struct RealEnv;
impl RunStateEnv for RealEnv {
async fn probe_service_version(&self) -> Result<ServiceVersionReply> {
// 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| {
+25 -6
View File
@@ -242,7 +242,7 @@ fn macos_service_install_marker_exists() -> std::io::Result<bool> {
}
#[cfg(windows)]
pub(crate) fn trusted_service_evidence() -> Result<bool> {
fn open_registered_service() -> Result<Option<windows_service::service::Service>> {
use windows_service::{
Error as WindowsServiceError,
service::ServiceAccess,
@@ -255,17 +255,36 @@ pub(crate) fn trusted_service_evidence() -> Result<bool> {
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<bool> {
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<bool> {
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<bool> {
let unit = format!("{}.service", clash_verge_service_ipc::SERVICE_SLUG);