Skip to content

Commit 986f3db

Browse files
committed
Fix race condition in mounts cache
This change switches to using the WithCache decorator for mounts instead of keeping track of a cache locally. This addresses a race condition when using the mounts structure. Signed-off-by: Evan Lezar <[email protected]>
1 parent 98deb7e commit 986f3db

File tree

1 file changed

+2
-17
lines changed

1 file changed

+2
-17
lines changed

internal/discover/mounts.go

+2-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"path/filepath"
2222
"strings"
23-
"sync"
2423

2524
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2625
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
@@ -35,15 +34,13 @@ type mounts struct {
3534
lookup lookup.Locator
3635
root string
3736
required []string
38-
sync.Mutex
39-
cache []Mount
4037
}
4138

4239
var _ Discover = (*mounts)(nil)
4340

4441
// NewMounts creates a discoverer for the required mounts using the specified locator.
4542
func NewMounts(logger logger.Interface, lookup lookup.Locator, root string, required []string) Discover {
46-
return newMounts(logger, lookup, root, required)
43+
return WithCache(newMounts(logger, lookup, root, required))
4744
}
4845

4946
// newMounts creates a discoverer for the required mounts using the specified locator.
@@ -60,15 +57,6 @@ func (d *mounts) Mounts() ([]Mount, error) {
6057
if d.lookup == nil {
6158
return nil, fmt.Errorf("no lookup defined")
6259
}
63-
64-
if d.cache != nil {
65-
d.logger.Debugf("returning cached mounts")
66-
return d.cache, nil
67-
}
68-
69-
d.Lock()
70-
defer d.Unlock()
71-
7260
uniqueMounts := make(map[string]Mount)
7361

7462
for _, candidate := range d.required {
@@ -112,10 +100,7 @@ func (d *mounts) Mounts() ([]Mount, error) {
112100
for _, m := range uniqueMounts {
113101
mounts = append(mounts, m)
114102
}
115-
116-
d.cache = mounts
117-
118-
return d.cache, nil
103+
return mounts, nil
119104
}
120105

121106
// relativeTo returns the path relative to the root for the file locator

0 commit comments

Comments
 (0)