Skip to content

Commit f4d863c

Browse files
authored
add ticker to uninstall RemovePath and error explain what was attempted (#3249)
1 parent 65ac6d5 commit f4d863c

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

internal/pkg/agent/install/uninstall.go

+33-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/elastic/elastic-agent/internal/pkg/config"
2525
"github.com/elastic/elastic-agent/internal/pkg/config/operations"
2626
"github.com/elastic/elastic-agent/pkg/component"
27-
comprt "github.com/elastic/elastic-agent/pkg/component/runtime"
27+
compruntime "github.com/elastic/elastic-agent/pkg/component/runtime"
2828
"github.com/elastic/elastic-agent/pkg/core/logger"
2929
"github.com/elastic/elastic-agent/pkg/features"
3030
)
@@ -97,31 +97,51 @@ func Uninstall(cfgFile, topPath, uninstallToken string) error {
9797
// to an ERROR_SHARING_VIOLATION. RemovePath will retry up to 2
9898
// seconds if it keeps getting that error.
9999
func RemovePath(path string) error {
100-
const arbitraryTimeout = 5 * time.Second
100+
if err := removePath(path); err != nil &&
101+
!isRetryableError(err) {
102+
return fmt.Errorf("could not remove %q, unretriable error: %w", path, err)
103+
}
104+
105+
const arbitraryTimeout = 7 * time.Second
106+
const nextSleep = 100 * time.Millisecond
107+
t := time.NewTicker(nextSleep)
108+
defer t.Stop()
101109
start := time.Now()
102-
nextSleep := 1 * time.Millisecond
110+
111+
var count int
103112
for {
104-
err := os.RemoveAll(path)
105-
if err == nil {
106-
return nil
107-
}
108-
if isBlockingOnExe(err) {
109-
// try to remove the blocking exe
110-
err = removeBlockingExe(err)
111-
}
113+
count++
114+
err := removePath(path)
112115
if err == nil {
113116
return nil
114117
}
115118
if !isRetryableError(err) {
116119
return err
117120
}
118121

122+
<-t.C
119123
if d := time.Since(start) + nextSleep; d >= arbitraryTimeout {
120-
return err
124+
return fmt.Errorf("could not remove path, "+
125+
"timeout exeeded after %d tries during %s. Last error: %v",
126+
count, arbitraryTimeout, err)
121127
}
122128
}
123129
}
124130

131+
func removePath(path string) error {
132+
err := os.RemoveAll(path)
133+
if err == nil {
134+
return nil
135+
}
136+
137+
if isBlockingOnExe(err) {
138+
// try to remove the blocking exe
139+
err = removeBlockingExe(err)
140+
}
141+
142+
return err
143+
}
144+
125145
func RemoveBut(path string, bestEffort bool, exceptions ...string) error {
126146
if len(exceptions) == 0 {
127147
return RemovePath(path)
@@ -232,7 +252,7 @@ func uninstallServiceComponent(ctx context.Context, log *logp.Logger, comp compo
232252
// Do not use infinite retries when uninstalling from the command line. If the uninstall needs to be
233253
// retried the entire uninstall command can be retried. Retries may complete asynchronously with the
234254
// execution of the uninstall command, leading to bugs like https://github.com/elastic/elastic-agent/issues/3060.
235-
return comprt.UninstallService(ctx, log, comp, uninstallToken)
255+
return compruntime.UninstallService(ctx, log, comp, uninstallToken)
236256
}
237257

238258
func serviceComponentsFromConfig(specs component.RuntimeSpecs, cfg *config.Config) ([]component.Component, error) {

0 commit comments

Comments
 (0)