From 49e6cb970a2efe7ded60be47a7b1bd62440ed031 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 29 Aug 2023 15:29:35 -0400 Subject: [PATCH] correctly identify retryable errors when uninstalling on Windows (#3317) (#3324) (cherry picked from commit 97272554d25afb5b68d6db94dd31664369511d12) Co-authored-by: Anderson Queiroz --- ...s-when-uninstalling-the-Elastic-Agent.yaml | 32 ++++++++++++++++++ internal/pkg/agent/install/uninstall.go | 4 +-- internal/pkg/agent/install/uninstall_unix.go | 2 +- .../pkg/agent/install/uninstall_windows.go | 33 ++++++++++++------- 4 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 changelog/fragments/1693319952-Change-error-handling-o-Windows-when-uninstalling-the-Elastic-Agent.yaml diff --git a/changelog/fragments/1693319952-Change-error-handling-o-Windows-when-uninstalling-the-Elastic-Agent.yaml b/changelog/fragments/1693319952-Change-error-handling-o-Windows-when-uninstalling-the-Elastic-Agent.yaml new file mode 100644 index 00000000000..3fa508590ff --- /dev/null +++ b/changelog/fragments/1693319952-Change-error-handling-o-Windows-when-uninstalling-the-Elastic-Agent.yaml @@ -0,0 +1,32 @@ +# Kind can be one of: +# - breaking-change: a change to previously-documented behavior +# - deprecation: functionality that is being removed in a later release +# - bug-fix: fixes a problem in a previous version +# - enhancement: extends functionality but does not break or fix existing behavior +# - feature: new functionality +# - known-issue: problems that we are aware of in a given version +# - security: impacts on the security of a product or a user’s deployment. +# - upgrade: important information for someone upgrading from a prior version +# - other: does not fit into any of the other categories +kind: bug-fix + +# Change summary; a 80ish characters long description of the change. +summary: Correctly identify retryable errors when attempting to uninstall on Windows + +# Long description; in case the summary is not enough to describe the change +# this field accommodate a description without length limits. +# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. +#description: + +# Affected component; a word indicating the component this changeset affects. +component: Uninstaller + +# PR URL; optional; the PR number that added the changeset. +# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. +# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. +# Please provide it if you are adding a fragment for a different PR. +#pr: https://github.com/owner/repo/1234 + +# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). +# If not present is automatically filled by the tooling with the issue linked to the PR number. +#issue: https://github.com/owner/repo/1234 diff --git a/internal/pkg/agent/install/uninstall.go b/internal/pkg/agent/install/uninstall.go index a7977a172b5..5da0b60fac1 100644 --- a/internal/pkg/agent/install/uninstall.go +++ b/internal/pkg/agent/install/uninstall.go @@ -134,8 +134,8 @@ func removePath(path string) error { return nil } - if isBlockingOnExe(err) { - // try to remove the blocking exe + if isAccessDeniedError(err) { + // might be blocking on exe, then try to remove the blocking exe err = removeBlockingExe(err) } diff --git a/internal/pkg/agent/install/uninstall_unix.go b/internal/pkg/agent/install/uninstall_unix.go index d63d4bcf2d4..bf3dee4c0a3 100644 --- a/internal/pkg/agent/install/uninstall_unix.go +++ b/internal/pkg/agent/install/uninstall_unix.go @@ -6,7 +6,7 @@ package install -func isBlockingOnExe(_ error) bool { +func isAccessDeniedError(_ error) bool { return false } diff --git a/internal/pkg/agent/install/uninstall_windows.go b/internal/pkg/agent/install/uninstall_windows.go index 5339cd376b3..d68ce4772c0 100644 --- a/internal/pkg/agent/install/uninstall_windows.go +++ b/internal/pkg/agent/install/uninstall_windows.go @@ -16,26 +16,31 @@ import ( "golang.org/x/sys/windows" ) -func isBlockingOnExe(err error) bool { +func isAccessDeniedError(err error) bool { if err == nil { return false } - path, errno := getPathFromError(err) - if path == "" { - return false + + var errno syscall.Errno + if errors.As(err, &errno) { + return errno == syscall.ERROR_ACCESS_DENIED } - return errno == syscall.ERROR_ACCESS_DENIED + + return false } func isRetryableError(err error) bool { if err == nil { return false } - path, errno := getPathFromError(err) - if path == "" { - return false + + var errno syscall.Errno + if errors.As(err, &errno) { + return errno == syscall.ERROR_ACCESS_DENIED || + errno == windows.ERROR_SHARING_VIOLATION } - return errno == syscall.ERROR_ACCESS_DENIED || errno == windows.ERROR_SHARING_VIOLATION + + return false } func removeBlockingExe(blockingErr error) error { @@ -72,14 +77,20 @@ func removeBlockingExe(blockingErr error) error { return nil } -func getPathFromError(blockingErr error) (string, syscall.Errno) { +func getPathFromError(err error) (string, syscall.Errno) { var perr *fs.PathError - if errors.As(blockingErr, &perr) { + if errors.As(err, &perr) { var errno syscall.Errno if errors.As(perr.Err, &errno) { return perr.Path, errno } } + + var errno syscall.Errno + if errors.As(err, &errno) { + return "", errno + } + return "", 0 }