Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstall split tunnel service if it cannot be started #7697

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Line wrap the file at 100 chars. Th
#### macOS
- Fix routing issue caused by upgrading `tun`.

#### Windows
- Attempt to reinstall split tunnel driver if it cannot be started.


## [2025.4] - 2025-02-12
This release is identical to 2025.4-beta1
Expand Down
35 changes: 30 additions & 5 deletions talpid-core/src/split_tunnel/windows/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
path::Path,
time::Duration,
};
use talpid_types::ErrorExt;
use windows_service::{
service::{
Service, ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType, ServiceState,
Expand Down Expand Up @@ -41,9 +42,9 @@ pub enum Error {
#[error("Failed to install split tunnel driver")]
InstallService(#[source] windows_service::Error),

/// Failed to start ST service
#[error("Timed out waiting on service to start")]
StartTimeout,
/// Failed to wait for ST service status
#[error("Timed out waiting on service status")]
StatusTimeout,

/// Failed to connect to existing driver
#[error("Failed to open service handle")]
Expand Down Expand Up @@ -78,7 +79,21 @@ pub fn install_driver_if_required(resource_dir: &Path) -> Result<(), Error> {
}
};

start_and_wait_for_service(&service)
if let Err(error) = start_and_wait_for_service(&service) {
log::error!(
"{}",
error.display_chain_with_msg("Failed to start driver service. Attempting reinstall")
);

let _ = reset_driver(&service);
stop_service(&service)?;
let _ = service.delete();
drop(service);

return install_driver(&scm, &expected_syspath);
}

Ok(())
}

pub fn stop_driver_service() -> Result<(), Error> {
Expand Down Expand Up @@ -107,6 +122,16 @@ fn stop_service(service: &Service) -> Result<(), Error> {
wait_for_status(service, ServiceState::Stopped)
}

fn reset_driver(service: &Service) -> Result<(), Error> {
let status = service.query_status().map_err(Error::QueryServiceStatus)?;
if status.current_state == ServiceState::Running {
let old_handle =
super::driver::DeviceHandle::new_handle_only().map_err(Error::OpenHandle)?;
old_handle.reset().map_err(Error::ResetDriver)?;
}
Ok(())
}

fn install_driver(scm: &ServiceManager, syspath: &Path) -> Result<(), Error> {
log::debug!("Installing split tunnel driver");

Expand Down Expand Up @@ -158,7 +183,7 @@ fn wait_for_status(service: &Service, target_state: ServiceState) -> Result<(),
}

if initial_time.elapsed() >= WAIT_STATUS_TIMEOUT {
return Err(Error::StartTimeout);
return Err(Error::StatusTimeout);
}

std::thread::sleep(std::time::Duration::from_secs(1));
Expand Down
Loading