@@ -221,8 +221,21 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
221
221
}()
222
222
defer func () {
223
223
// 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
+
226
239
close (p .jobRunning )
227
240
}()
228
241
// 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)
279
292
err = nil
280
293
}
281
294
if err != nil {
295
+ _ = file .Close ()
282
296
go p .cancelActiveJobf ("copy file %q: %s" , path , err )
283
297
return
284
298
}
@@ -315,7 +329,11 @@ func (p *provisionerDaemon) runJob(ctx context.Context, job *proto.AcquiredJob)
315
329
return
316
330
}
317
331
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
+ }
319
337
}
320
338
321
339
func (p * provisionerDaemon ) runProjectImport (ctx context.Context , provisioner sdkproto.DRPCProvisionerClient , job * proto.AcquiredJob ) {
@@ -512,9 +530,6 @@ func (p *provisionerDaemon) closeWithError(err error) error {
512
530
p .cancelActiveJobf (errMsg )
513
531
p .closeCancel ()
514
532
515
- // Required until we're on Go 1.18. See:
516
- // https://github.com/golang/go/issues/50510
517
- _ = os .RemoveAll (p .opts .WorkDirectory )
518
533
p .opts .Logger .Debug (context .Background (), "closing server with error" , slog .Error (err ))
519
534
520
535
return err
0 commit comments