Skip to content

test: fix cleanup order on provisioner daemon work dir #9668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,20 @@ func NewWithAPI(t testing.TB, options *Options) (*codersdk.Client, io.Closer, *c
func NewProvisionerDaemon(t testing.TB, coderAPI *coderd.API) io.Closer {
t.Helper()

// t.Cleanup runs in last added, first called order. t.TempDir() will delete
// the directory on cleanup, so we want to make sure the echoServer is closed
// before we go ahead an attempt to delete it's work directory.
// seems t.TempDir() is not safe to call from a different goroutine
workDir := t.TempDir()

echoClient, echoServer := provisionersdk.MemTransportPipe()
ctx, cancelFunc := context.WithCancel(context.Background())
t.Cleanup(func() {
_ = echoClient.Close()
_ = echoServer.Close()
cancelFunc()
})
// seems t.TempDir() is not safe to call from a different goroutine
workDir := t.TempDir()

go func() {
err := echo.Serve(ctx, &provisionersdk.ServeOptions{
Listener: echoServer,
Expand Down
9 changes: 9 additions & 0 deletions provisionersdk/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ func (s *Session) extractArchive() error {
if mode == 0 {
mode = 0o600
}

// Always check for context cancellation before reading the next header.
// This is mainly important for unit tests, since a canceled context means
// the underlying directory is going to be deleted. There still exists
// the small race condition that the context is cancelled after this, and
// before the disk write.
if ctx.Err() != nil {
return xerrors.Errorf("context canceled: %w", ctx.Err())
}
switch header.Typeflag {
case tar.TypeDir:
err = os.MkdirAll(headerPath, mode)
Expand Down