From 28fec2b9cb2641cdc668e1fb93f4a205842ffb0c Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 31 Aug 2018 11:59:53 -0700 Subject: [PATCH 1/2] cache: fix possible prune deadlock Signed-off-by: Tonis Tiigi --- cache/manager.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cache/manager.go b/cache/manager.go index a6a3f86e..6bfb8abc 100644 --- a/cache/manager.go +++ b/cache/manager.go @@ -360,10 +360,10 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt gcMode := opt.keepBytes != 0 cutOff := time.Now().Add(-opt.keepDuration) - locked := map[*cacheRecord]struct{}{} + locked := map[*sync.Mutex]struct{}{} for _, cr := range cm.records { - if _, ok := locked[cr]; ok { + if _, ok := locked[cr.mu]; ok { continue } cr.mu.Lock() @@ -431,7 +431,7 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt return err } } else { - locked[cr] = struct{}{} + locked[cr.mu] = struct{}{} continue // leave the record locked } } From acab0f68738cf9c436c68d608372d4530b12cf74 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 31 Aug 2018 12:00:13 -0700 Subject: [PATCH 2/2] cache: correct prune size calculation cache Signed-off-by: Tonis Tiigi --- cache/manager.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cache/manager.go b/cache/manager.go index 6bfb8abc..7939cce1 100644 --- a/cache/manager.go +++ b/cache/manager.go @@ -454,7 +454,6 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt return err } toDelete = toDelete[:1] - opt.totalSize -= getSize(toDelete[0].md) } cm.mu.Unlock() @@ -483,7 +482,9 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt if cr.parent != nil { c.Parent = cr.parent.ID() } - + if c.Size == sizeUnknown && cr.equalImmutable != nil { + c.Size = getSize(cr.equalImmutable.md) // benefit from DiskUsage calc + } if c.Size == sizeUnknown { cr.mu.Unlock() // all the non-prune modifications already protected by cr.dead s, err := cr.Size(ctx) @@ -494,6 +495,8 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt cr.mu.Lock() } + opt.totalSize -= c.Size + if cr.equalImmutable != nil { if err1 := cr.equalImmutable.remove(ctx, false); err == nil { err = err1