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 race condition in mounts cache #737

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 17 deletions internal/discover/mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"path/filepath"
"strings"
"sync"

"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
Expand All @@ -35,15 +34,13 @@ type mounts struct {
lookup lookup.Locator
root string
required []string
sync.Mutex
cache []Mount
}

var _ Discover = (*mounts)(nil)

// NewMounts creates a discoverer for the required mounts using the specified locator.
func NewMounts(logger logger.Interface, lookup lookup.Locator, root string, required []string) Discover {
return newMounts(logger, lookup, root, required)
return WithCache(newMounts(logger, lookup, root, required))
}

// newMounts creates a discoverer for the required mounts using the specified locator.
Expand All @@ -60,15 +57,6 @@ func (d *mounts) Mounts() ([]Mount, error) {
if d.lookup == nil {
return nil, fmt.Errorf("no lookup defined")
}

if d.cache != nil {
d.logger.Debugf("returning cached mounts")
return d.cache, nil
}

d.Lock()
defer d.Unlock()

uniqueMounts := make(map[string]Mount)

for _, candidate := range d.required {
Expand Down Expand Up @@ -112,10 +100,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
for _, m := range uniqueMounts {
mounts = append(mounts, m)
}

d.cache = mounts

return d.cache, nil
return mounts, nil
}

// relativeTo returns the path relative to the root for the file locator
Expand Down