Skip to content

Commit b6d27ad

Browse files
authored
fix: Attempt cleanup multiple times on Windows (#161)
See: golang/go#50510
1 parent 2eab1b5 commit b6d27ad

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

provisionerd/provisionerd.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,21 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
221221
}()
222222
defer func() {
223223
// Cleanup the work directory after execution.
224-
err := os.RemoveAll(p.opts.WorkDirectory)
225-
p.opts.Logger.Debug(ctx, "cleaned up work directory", slog.Error(err))
224+
for attempt := 0; attempt < 5; attempt++ {
225+
err := os.RemoveAll(p.opts.WorkDirectory)
226+
if err != nil {
227+
// On Windows, open files cannot be removed.
228+
// When the provisioner daemon is shutting down,
229+
// it may take a few milliseconds for processes to exit.
230+
// See: https://github.com/golang/go/issues/50510
231+
p.opts.Logger.Debug(ctx, "failed to clean work directory; trying again", slog.Error(err))
232+
time.Sleep(250 * time.Millisecond)
233+
continue
234+
}
235+
p.opts.Logger.Debug(ctx, "cleaned up work directory", slog.Error(err))
236+
break
237+
}
238+
226239
close(p.jobRunning)
227240
}()
228241
// It's safe to cast this ProvisionerType. This data is coming directly from coderd.
@@ -279,6 +292,7 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
279292
err = nil
280293
}
281294
if err != nil {
295+
_ = file.Close()
282296
go p.cancelActiveJobf("copy file %q: %s", path, err)
283297
return
284298
}
@@ -315,7 +329,11 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
315329
return
316330
}
317331

318-
p.opts.Logger.Info(context.Background(), "completed job", slog.F("id", job.JobId))
332+
// Ensure the job is still running to output.
333+
// It's possible the job was canceled.
334+
if p.isRunningJob() {
335+
p.opts.Logger.Info(context.Background(), "completed job", slog.F("id", job.JobId))
336+
}
319337
}
320338

321339
func (p *provisionerDaemon) runProjectImport(ctx context.Context, provisioner sdkproto.DRPCProvisionerClient, job *proto.AcquiredJob) {
@@ -512,9 +530,6 @@ func (p *provisionerDaemon) closeWithError(err error) error {
512530
p.cancelActiveJobf(errMsg)
513531
p.closeCancel()
514532

515-
// Required until we're on Go 1.18. See:
516-
// https://github.com/golang/go/issues/50510
517-
_ = os.RemoveAll(p.opts.WorkDirectory)
518533
p.opts.Logger.Debug(context.Background(), "closing server with error", slog.Error(err))
519534

520535
return err

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy