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 <mhjlq1989@gmail.com>
pull/31630/head
haojingcn 2 weeks ago
parent 4b6a4b6932
commit 080709c195

@ -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 {

@ -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)
}

Loading…
Cancel
Save