Skip to content

Commit

Permalink
fix: Attempt cleanup multiple times on Windows (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarbs committed Feb 4, 2022
1 parent 2eab1b5 commit b6d27ad
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions provisionerd/provisionerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,21 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
}()
defer func() {
// Cleanup the work directory after execution.
err := os.RemoveAll(p.opts.WorkDirectory)
p.opts.Logger.Debug(ctx, "cleaned up work directory", slog.Error(err))
for attempt := 0; attempt < 5; attempt++ {
err := os.RemoveAll(p.opts.WorkDirectory)
if err != nil {
// On Windows, open files cannot be removed.
// When the provisioner daemon is shutting down,
// it may take a few milliseconds for processes to exit.
// See: https://github.com/golang/go/issues/50510
p.opts.Logger.Debug(ctx, "failed to clean work directory; trying again", slog.Error(err))
time.Sleep(250 * time.Millisecond)
continue
}
p.opts.Logger.Debug(ctx, "cleaned up work directory", slog.Error(err))
break
}

close(p.jobRunning)
}()
// It's safe to cast this ProvisionerType. This data is coming directly from coderd.
Expand Down Expand Up @@ -279,6 +292,7 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
err = nil
}
if err != nil {
_ = file.Close()
go p.cancelActiveJobf("copy file %q: %s", path, err)
return
}
Expand Down Expand Up @@ -315,7 +329,11 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
return
}

p.opts.Logger.Info(context.Background(), "completed job", slog.F("id", job.JobId))
// Ensure the job is still running to output.
// It's possible the job was canceled.
if p.isRunningJob() {
p.opts.Logger.Info(context.Background(), "completed job", slog.F("id", job.JobId))
}
}

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

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

return err
Expand Down

0 comments on commit b6d27ad

Please sign in to comment.