Skip to content

Commit

Permalink
fix: file download race condition.
Browse files Browse the repository at this point in the history
Ignore a file exists error when attempting to rename a downloaded file that was downloaded meanwhile by another process.

Closes #36
  • Loading branch information
mgoltzsche committed Jul 14, 2022
1 parent 79ecd15 commit 62b60dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/helm/locate.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func locateChart(ctx context.Context, cfg *config.LoaderConfig, repos repository
return
}
err = os.Rename(tmpDestDir, destDir)
if os.IsExist(err) {
// Ignore error if file was downloaded by another process concurrently.
// This fixes a race condition, see https://github.com/mgoltzsche/khelm/issues/36
err = os.RemoveAll(tmpDestDir)
}
err = errors.WithStack(err)
}()
select {
Expand Down
5 changes: 5 additions & 0 deletions pkg/helm/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ func downloadIndexFile(ctx context.Context, entry *repo.Entry, cacheDir string,
return
}
err = os.Rename(tmpIdxFileName, idxFile)
if os.IsExist(err) {
// Ignore error if file was downloaded by another process concurrently.
// This fixes a race condition, see https://github.com/mgoltzsche/khelm/issues/36
err = os.Remove(tmpIdxFileName)
}
err = errors.WithStack(err)
}()
select {
Expand Down

0 comments on commit 62b60dd

Please sign in to comment.