diff --git a/builder/builder-next/adapters/snapshot/snapshot.go b/builder/builder-next/adapters/snapshot/snapshot.go index e5e6942653..c1388da7e9 100644 --- a/builder/builder-next/adapters/snapshot/snapshot.go +++ b/builder/builder-next/adapters/snapshot/snapshot.go @@ -426,10 +426,11 @@ func (s *snapshotter) Close() error { } type mountable struct { - mu sync.Mutex - mounts []mount.Mount - acquire func() ([]mount.Mount, error) - release func() error + mu sync.Mutex + mounts []mount.Mount + acquire func() ([]mount.Mount, error) + release func() error + refCount int } func (m *mountable) Mount() ([]mount.Mount, error) { @@ -437,6 +438,7 @@ func (m *mountable) Mount() ([]mount.Mount, error) { defer m.mu.Unlock() if m.mounts != nil { + m.refCount++ return m.mounts, nil } @@ -445,6 +447,7 @@ func (m *mountable) Mount() ([]mount.Mount, error) { return nil, err } m.mounts = mounts + m.refCount = 1 return m.mounts, nil } @@ -452,6 +455,13 @@ func (m *mountable) Mount() ([]mount.Mount, error) { func (m *mountable) Release() error { m.mu.Lock() defer m.mu.Unlock() + + if m.refCount > 1 { + m.refCount-- + return nil + } + + m.refCount = 0 if m.release == nil { return nil }