@@ -24,7 +24,7 @@ import (
24
24
"github.com/elastic/elastic-agent/internal/pkg/config"
25
25
"github.com/elastic/elastic-agent/internal/pkg/config/operations"
26
26
"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"
28
28
"github.com/elastic/elastic-agent/pkg/core/logger"
29
29
"github.com/elastic/elastic-agent/pkg/features"
30
30
)
@@ -97,31 +97,51 @@ func Uninstall(cfgFile, topPath, uninstallToken string) error {
97
97
// to an ERROR_SHARING_VIOLATION. RemovePath will retry up to 2
98
98
// seconds if it keeps getting that error.
99
99
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 ()
101
109
start := time .Now ()
102
- nextSleep := 1 * time .Millisecond
110
+
111
+ var count int
103
112
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 )
112
115
if err == nil {
113
116
return nil
114
117
}
115
118
if ! isRetryableError (err ) {
116
119
return err
117
120
}
118
121
122
+ <- t .C
119
123
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 )
121
127
}
122
128
}
123
129
}
124
130
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
+
125
145
func RemoveBut (path string , bestEffort bool , exceptions ... string ) error {
126
146
if len (exceptions ) == 0 {
127
147
return RemovePath (path )
@@ -232,7 +252,7 @@ func uninstallServiceComponent(ctx context.Context, log *logp.Logger, comp compo
232
252
// Do not use infinite retries when uninstalling from the command line. If the uninstall needs to be
233
253
// retried the entire uninstall command can be retried. Retries may complete asynchronously with the
234
254
// 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 )
236
256
}
237
257
238
258
func serviceComponentsFromConfig (specs component.RuntimeSpecs , cfg * config.Config ) ([]component.Component , error ) {
0 commit comments