diff --git a/pkg/extkingpin/path_content_reloader.go b/pkg/extkingpin/path_content_reloader.go index e0d8fdf74ba..e96b0ddb34a 100644 --- a/pkg/extkingpin/path_content_reloader.go +++ b/pkg/extkingpin/path_content_reloader.go @@ -8,6 +8,7 @@ import ( "crypto/sha256" "os" "path/filepath" + "sync" "time" "github.com/go-kit/log" @@ -79,15 +80,22 @@ func (p *pollingEngine) start(ctx context.Context) error { } type staticPathContent struct { - content []byte - path string + contentMtx sync.Mutex + content []byte + path string } var _ fileContent = (*staticPathContent)(nil) // Content returns the cached content. func (t *staticPathContent) Content() ([]byte, error) { - return t.content, nil + t.contentMtx.Lock() + defer t.contentMtx.Unlock() + + c := make([]byte, 0, len(t.content)) + c = append(c, t.content...) + + return c, nil } // Path returns the path to the file that contains the content. @@ -102,12 +110,15 @@ func NewStaticPathContent(fromPath string) (*staticPathContent, error) { if err != nil { return nil, errors.Wrapf(err, "could not load test content: %s", fromPath) } - return &staticPathContent{content, fromPath}, nil + return &staticPathContent{content: content, path: fromPath}, nil } // Rewrite rewrites the file backing this staticPathContent and swaps the local content cache. The file writing // is needed to trigger the file system monitor. func (t *staticPathContent) Rewrite(newContent []byte) error { + t.contentMtx.Lock() + defer t.contentMtx.Unlock() + t.content = newContent // Write the file to ensure possible file watcher reloaders get triggered. return os.WriteFile(t.path, newContent, 0666)