Skip to content

Commit 0e4d689

Browse files
authored
test: fix cleanup order on provisioner daemon work dir (#9668)
* test: fix cleanup order on provisioner daemon work dir * Reduce the test race condition
1 parent 53a985f commit 0e4d689

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

coderd/coderdtest/coderdtest.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,20 @@ func NewWithAPI(t testing.TB, options *Options) (*codersdk.Client, io.Closer, *c
459459
func NewProvisionerDaemon(t testing.TB, coderAPI *coderd.API) io.Closer {
460460
t.Helper()
461461

462+
// t.Cleanup runs in last added, first called order. t.TempDir() will delete
463+
// the directory on cleanup, so we want to make sure the echoServer is closed
464+
// before we go ahead an attempt to delete it's work directory.
465+
// seems t.TempDir() is not safe to call from a different goroutine
466+
workDir := t.TempDir()
467+
462468
echoClient, echoServer := provisionersdk.MemTransportPipe()
463469
ctx, cancelFunc := context.WithCancel(context.Background())
464470
t.Cleanup(func() {
465471
_ = echoClient.Close()
466472
_ = echoServer.Close()
467473
cancelFunc()
468474
})
469-
// seems t.TempDir() is not safe to call from a different goroutine
470-
workDir := t.TempDir()
475+
471476
go func() {
472477
err := echo.Serve(ctx, &provisionersdk.ServeOptions{
473478
Listener: echoServer,

provisionersdk/session.go

+9
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ func (s *Session) extractArchive() error {
230230
if mode == 0 {
231231
mode = 0o600
232232
}
233+
234+
// Always check for context cancellation before reading the next header.
235+
// This is mainly important for unit tests, since a canceled context means
236+
// the underlying directory is going to be deleted. There still exists
237+
// the small race condition that the context is cancelled after this, and
238+
// before the disk write.
239+
if ctx.Err() != nil {
240+
return xerrors.Errorf("context canceled: %w", ctx.Err())
241+
}
233242
switch header.Typeflag {
234243
case tar.TypeDir:
235244
err = os.MkdirAll(headerPath, mode)

0 commit comments

Comments
 (0)