From 7352a90b950b20e857ba3d1e693f0ba3b88c3c75 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Fri, 11 Oct 2024 15:56:35 +0200 Subject: [PATCH] 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 --- internal/discover/mounts.go | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/internal/discover/mounts.go b/internal/discover/mounts.go index 9232dae3..7241c5ad 100644 --- a/internal/discover/mounts.go +++ b/internal/discover/mounts.go @@ -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" @@ -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. @@ -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 { @@ -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