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

fix data race in index cache #1369

Merged
merged 2 commits into from
Oct 25, 2024
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
6 changes: 6 additions & 0 deletions pkg/apk/apk/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type indexCache struct {
// For remote indexes.
onces sync.Map
urlToEtag map[string]string
etagMu sync.Mutex // guards urlToEtag

// For local indexes.
sync.Mutex
Expand Down Expand Up @@ -151,17 +152,22 @@ func (i *indexCache) get(ctx context.Context, repoName, repoURL string, keys map
once, _ := i.onces.LoadOrStore(key, &sync.Once{})
once.(*sync.Once).Do(func() {
// If we've seen this URL before, delete any references to old indexes so we can GC them.
// Lock reads/writes to the map, without blocking the fetchAndParse goroutine.
i.etagMu.Lock()
prev, ok := i.urlToEtag[u]
if ok {
prevKey := fmt.Sprintf("%s@%s", u, prev)
i.forget(prevKey)
}
i.etagMu.Unlock()

idx, err := fetchAndParse(etag)
i.store(key, idx, err)

// Record the current etag for this URL so we can GC it later.
i.etagMu.Lock()
i.urlToEtag[u] = etag
i.etagMu.Unlock()
})

return i.load(key)
Expand Down
Loading