From e6e6e4347d7d1cb04223efd7ef687d0cbc47780e Mon Sep 17 00:00:00 2001 From: Amir Saeid Date: Tue, 22 May 2018 15:37:28 +0100 Subject: [PATCH] Ask ioutil to write in a temp file Similar to https://github.com/kubernetes/helm/issues/3253 we're using Helm by directly importing the Go package. A concurrency issue arises when multiple requests come for the same package. Currently DownloadTo truncates contents of destination file and rewrites it. This patch changes the behaviour so instead of directly writing into the destination it'll write the content in a temp file and renames it once download is finished. This change fixes sporadic issues in https://github.com/amir/skeg/blob/5c5c6a1c215cf5110843a28f1fa389aa69a3f452/main_test.go --- pkg/downloader/chart_downloader.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index 59b9d4d75..a6e0a4466 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -97,7 +97,9 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven name := filepath.Base(u.Path) destfile := filepath.Join(dest, name) - if err := ioutil.WriteFile(destfile, data.Bytes(), 0644); err != nil { + tempdestfile := destfile + ".part" + if err := ioutil.WriteFile(tempdestfile, data.Bytes(), 0644); err != nil { + err = os.Rename(tempdestfile, destfile) return destfile, nil, err }