Skip to content

Commit

Permalink
Use os.MkdirAll, which is thread-safe (#849) (#850)
Browse files Browse the repository at this point in the history
The same race condition does happen in the implementation of
os.MkdirAll[0], but that code there takes it into account and re-checks
the second error (the one on Mkdir), and it ends up returning nil if the
directory is indeed created, thus avoiding the race condition and making
os.MkdirAll thread-safe.

[0] https://github.com/golang/go/blob/e33f7c42b084182a3a88ef79857e33c11627159a/src/os/path.go#L19-L66
  • Loading branch information
agarciamontoro authored Nov 20, 2024
1 parent 286a3f7 commit ef86d90
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions deployment/terraform/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,7 @@ func New(id string, cfg deployment.Config) (*Terraform, error) {
}

func ensureTerraformStateDir(dir string) error {
// Make sure that the state directory exists
_, err := os.Stat(dir)
if err == nil {
return nil
}

// Return any error different than the one showing
// that the directory does not exist
if !errors.Is(err, os.ErrNotExist) {
return err
}

// If it does not exist, create it
return os.Mkdir(dir, 0700)
return os.MkdirAll(dir, 0700)
}

// Create creates a new load test environment.
Expand Down

0 comments on commit ef86d90

Please sign in to comment.