Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extkingpin: fix Content/Rewrite race #6870

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions pkg/extkingpin/path_content_reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/sha256"
"os"
"path/filepath"
"sync"
"time"

"github.com/go-kit/log"
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
Loading