From 21ce03cff27d4460450237a3a52975f97ccf7974 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 26 May 2024 12:38:20 +0200 Subject: [PATCH] bloblru: move defer outside critical section --- internal/bloblru/cache.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/bloblru/cache.go b/internal/bloblru/cache.go index 1ff52094b..161f15375 100644 --- a/internal/bloblru/cache.go +++ b/internal/bloblru/cache.go @@ -100,7 +100,13 @@ func (c *Cache) GetOrCompute(id restic.ID, compute func() ([]byte, error)) ([]by waitForResult, isDownloading := c.inProgress[id] if !isDownloading { c.inProgress[id] = finish + } + c.mu.Unlock() + if isDownloading { + // wait for result of parallel download + <-waitForResult + } else { // remove progress channel once finished here defer func() { c.mu.Lock() @@ -109,12 +115,6 @@ func (c *Cache) GetOrCompute(id restic.ID, compute func() ([]byte, error)) ([]by close(finish) }() } - c.mu.Unlock() - - if isDownloading { - // wait for result of parallel download - <-waitForResult - } // try again. This is necessary independent of whether isDownloading is true or not. // The calls to `c.Get()` and checking/adding the entry in `c.inProgress` are not atomic,