From 080709c195cc0c83b671efb5d9ce3e197b7fc008 Mon Sep 17 00:00:00 2001 From: haojingcn Date: Mon, 15 Dec 2025 14:39:16 +0800 Subject: [PATCH] Fix: Data Race Bug detected via TestConcurrenyDownloadIndex. use a ChartRepository per go-routine, to avoid concurrent operation on the same ChartRepository instance. Signed-off-by: haojingcn --- pkg/repo/v1/chartrepo.go | 4 ---- pkg/repo/v1/chartrepo_test.go | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/repo/v1/chartrepo.go b/pkg/repo/v1/chartrepo.go index acbede2f7..24a2bab1c 100644 --- a/pkg/repo/v1/chartrepo.go +++ b/pkg/repo/v1/chartrepo.go @@ -28,7 +28,6 @@ import ( "os" "path/filepath" "strings" - "sync" "helm.sh/helm/v4/internal/fileutil" "helm.sh/helm/v4/pkg/getter" @@ -54,7 +53,6 @@ type ChartRepository struct { IndexFile *IndexFile Client getter.Getter CachePath string - mutex sync.RWMutex } // NewChartRepository constructs ChartRepository @@ -79,8 +77,6 @@ func NewChartRepository(cfg *Entry, getters getter.Providers) (*ChartRepository, // DownloadIndexFile fetches the index from a repository. func (r *ChartRepository) DownloadIndexFile() (string, error) { - r.mutex.Lock() - defer r.mutex.Unlock() indexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml") if err != nil { diff --git a/pkg/repo/v1/chartrepo_test.go b/pkg/repo/v1/chartrepo_test.go index a707cf36d..8a8059910 100644 --- a/pkg/repo/v1/chartrepo_test.go +++ b/pkg/repo/v1/chartrepo_test.go @@ -101,10 +101,13 @@ func TestConcurrenyDownloadIndex(t *testing.T) { } defer srv.Close() - repo, err := NewChartRepository(&Entry{ + // set base environment settings + baseEntry := &Entry{ Name: "nginx", URL: srv.URL, - }, getter.All(&cli.EnvSettings{})) + } + settings := cli.EnvSettings{} + repo, err := NewChartRepository(baseEntry, getter.All(&settings)) if err != nil { t.Fatalf("Problem loading chart repository from %s: %v", srv.URL, err) @@ -130,7 +133,14 @@ func TestConcurrenyDownloadIndex(t *testing.T) { go func() { defer wg.Done() - idx, err := repo.DownloadIndexFile() + localRepo, err := NewChartRepository(baseEntry, getter.All(&settings)) + if err != nil { + t.Errorf("Failed to create chart repository: %v", err) + return + } + localRepo.CachePath = repo.CachePath + + idx, err := localRepo.DownloadIndexFile() if err != nil { t.Errorf("Failed to download index file to %s: %v", idx, err) }