Skip to content

Commit

Permalink
Merge pull request #1340 from lcarva/EC-414
Browse files Browse the repository at this point in the history
Fix 'concurrent map writes' error
  • Loading branch information
lcarva authored Feb 12, 2024
2 parents 4d8c979 + d71a5cb commit a1a29bd
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"regexp"
"strings"
"sync"

"github.com/open-policy-agent/conftest/downloader"
log "github.com/sirupsen/logrus"
Expand All @@ -36,6 +37,8 @@ type downloadImpl interface {
Download(context.Context, string, []string) error
}

var dlMutex sync.Mutex

// WithDownloadImpl replaces the downloadImpl implementation used
func WithDownloadImpl(ctx context.Context, d downloadImpl) context.Context {
return context.WithValue(ctx, downloadImplKey, d)
Expand All @@ -59,7 +62,13 @@ func Download(ctx context.Context, destDir string, sourceUrl string, showMsg boo
if d, ok := ctx.Value(downloadImplKey).(downloadImpl); ok {
err = d.Download(ctx, destDir, []string{sourceUrl})
} else {
// conftest's Download function leverages oras under the hood to fetch from OCI. It uses the
// global oras client and sets the user agent to "conftest". This is not a thread safe
// operation. Here we get around this limitation by ensuring a single download happens at a
// time.
dlMutex.Lock()
err = downloader.Download(ctx, destDir, []string{sourceUrl})
dlMutex.Unlock()
}

if err != nil {
Expand Down

0 comments on commit a1a29bd

Please sign in to comment.