From 62e7eedb4bb51c7ccd22f7d053968cf682ed2a30 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Sun, 16 Jun 2024 14:30:27 +0300 Subject: [PATCH] fix(concurrency): add mutex to protect repoFailList and out in updateCharts Signed-off-by: Suleiman Dibirov --- cmd/helm/repo_update.go | 11 +++++++++-- cmd/helm/repo_update_test.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/helm/repo_update.go b/cmd/helm/repo_update.go index 8d5f532f1..f3b771fce 100644 --- a/cmd/helm/repo_update.go +++ b/cmd/helm/repo_update.go @@ -116,15 +116,22 @@ func (o *repoUpdateOptions) run(out io.Writer) error { func updateCharts(repos []*repo.ChartRepository, out io.Writer, failOnRepoUpdateFail bool) error { fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...") - var wg sync.WaitGroup - var repoFailList []string + + var ( + wg sync.WaitGroup + mu sync.Mutex + repoFailList []string + ) + for _, re := range repos { wg.Add(1) go func(re *repo.ChartRepository) { defer wg.Done() if _, err := re.DownloadIndexFile(); err != nil { + mu.Lock() fmt.Fprintf(out, "...Unable to get an update from the %q chart repository (%s):\n\t%s\n", re.Config.Name, re.Config.URL, err) repoFailList = append(repoFailList, re.Config.URL) + mu.Unlock() } else { fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name) } diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index 1ddf0f5ed..d12b328bc 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -205,7 +205,14 @@ func TestUpdateChartsFailWithError(t *testing.T) { defer ts.Stop() var invalidURL = ts.URL() + "55" - r, err := repo.NewChartRepository(&repo.Entry{ + r1, err := repo.NewChartRepository(&repo.Entry{ + Name: "charts", + URL: invalidURL, + }, getter.All(settings)) + if err != nil { + t.Error(err) + } + r2, err := repo.NewChartRepository(&repo.Entry{ Name: "charts", URL: invalidURL, }, getter.All(settings)) @@ -214,7 +221,7 @@ func TestUpdateChartsFailWithError(t *testing.T) { } b := bytes.NewBuffer(nil) - err = updateCharts([]*repo.ChartRepository{r}, b, true) + err = updateCharts([]*repo.ChartRepository{r1, r2}, b, true) if err == nil { t.Error("Repo update should return error because update of repository fails and 'fail-on-repo-update-fail' flag set") return