From 58bbf651dee79704378750863ae4cabd4d23d729 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Thu, 6 Jun 2024 19:22:49 +0530 Subject: [PATCH] fix(manager): ignore NotADirectory error when uninstalling services --- sn_node_manager/src/lib.rs | 5 ++++- sn_service_management/src/control.rs | 21 ++++++++++++++------- sn_service_management/src/error.rs | 2 ++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/sn_node_manager/src/lib.rs b/sn_node_manager/src/lib.rs index b07f810a34..0dc6debdba 100644 --- a/sn_node_manager/src/lib.rs +++ b/sn_node_manager/src/lib.rs @@ -256,13 +256,16 @@ impl ServiceManager { } Err(err) => match err { ServiceError::ServiceRemovedManually(name) => { - warn!("The user appears to have removed the {name} service manually",); + warn!("The user appears to have removed the {name} service manually. Skipping the error.",); // The user has deleted the service definition file, which the service manager // crate treats as an error. We then return our own error type, which allows us // to handle it here and just proceed with removing the service from the // registry. println!("The user appears to have removed the {name} service manually"); } + ServiceError::ServiceDoesNotExists(name) => { + warn!("The service {name} has most probably been removed already, it does not exists. Skipping the error."); + } _ => { error!("Error uninstalling the service: {err}"); return Err(err.into()); diff --git a/sn_service_management/src/control.rs b/sn_service_management/src/control.rs index e3289fd1bf..29e1f07689 100644 --- a/sn_service_management/src/control.rs +++ b/sn_service_management/src/control.rs @@ -264,19 +264,26 @@ impl ServiceControl for ServiceController { } match manager.uninstall(ServiceUninstallCtx { label }) { Ok(()) => Ok(()), - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => { + Err(err) => { + if std::io::ErrorKind::NotFound == err.kind() { error!("Error while uninstall service, service file might have been removed manually: {service_name}"); // In this case the user has removed the service definition file manually, // which the service manager crate treats as an error. We can propagate the // it to the caller and they can decide how to handle it. Err(Error::ServiceRemovedManually(service_name.to_string())) + } else if err.raw_os_error() == Some(267) { + // This requires the unstable io_error_more feature, use raw code for now + // else if err.kind() == std::io::ErrorKind::NotADirectory {} + + // This happens on windows when the service has been already cleared, but was not updated + // in the registry. Happens when the Service application (in windows) is open while calling + // 'remove' or 'reset'. + Err(Error::ServiceDoesNotExists(service_name.to_string())) + } else { + error!("Error while uninstalling service: {err:?}"); + Err(err.into()) } - _ => { - error!("Error while uninstalling service: {e:?}"); - Err(e.into()) - } - }, + } } } diff --git a/sn_service_management/src/error.rs b/sn_service_management/src/error.rs index 726775ee69..c28a919b7a 100644 --- a/sn_service_management/src/error.rs +++ b/sn_service_management/src/error.rs @@ -45,6 +45,8 @@ pub enum Error { RpcRecordAddressError(String), #[error("Could not find process at '{0}'")] ServiceProcessNotFound(String), + #[error("The service '{0}' does not exists and cannot be removed.")] + ServiceDoesNotExists(String), #[error("The user may have removed the '{0}' service outwith the node manager")] ServiceRemovedManually(String), #[error("Failed to create service user account")]