diff --git a/store/cache/cache.go b/store/cache/cache.go index 310942e49..b7231267c 100644 --- a/store/cache/cache.go +++ b/store/cache/cache.go @@ -50,6 +50,10 @@ type Config struct { MaxItems int // OnEviction is called when an item is evicted from the cache. + // It runs synchronously in the goroutine performing the eviction, which may + // be the background cleanup goroutine. It must not call Close synchronously: + // Close waits for that goroutine to exit. To request closure from a callback, + // start Close in a separate goroutine and return without waiting for it. OnEviction func(key string, value any) } @@ -203,11 +207,13 @@ func (c *Cache) Size() int64 { } // Close stops the cache cleanup goroutine. +// Every caller waits for cleanup to finish. Close must not be called +// synchronously from OnEviction; see Config.OnEviction. func (c *Cache) Close() error { if c.closed.CompareAndSwap(false, true) { close(c.stopChan) - <-c.closedChan // Wait for cleanup goroutine to exit } + <-c.closedChan // Every caller waits for the cleanup goroutine to exit. return nil } diff --git a/store/cache/cache_test.go b/store/cache/cache_test.go index 3154eb5d5..54d3aaa45 100644 --- a/store/cache/cache_test.go +++ b/store/cache/cache_test.go @@ -6,6 +6,7 @@ import ( "sync" "sync/atomic" "testing" + "testing/synctest" "time" ) @@ -462,3 +463,82 @@ func TestCacheCloseWaitsForCleanup(t *testing.T) { t.Fatal("Close did not return after cleanup finished") } } + +func TestCloseWaitsForCleanupForEveryCaller(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + entered := make(chan struct{}) + release := make(chan struct{}) + config := DefaultConfig() + config.CleanupInterval = time.Second + config.OnEviction = func(string, any) { + close(entered) + <-release + } + cache := New(config) + cache.SetWithTTL(t.Context(), "expired", "value", -time.Second) + <-entered + first := make(chan struct{}) + go func() { _ = cache.Close(); close(first) }() + synctest.Wait() + second := make(chan struct{}) + go func() { _ = cache.Close(); close(second) }() + synctest.Wait() + select { + case <-second: + t.Error("Close returned before the cleanup goroutine finished") + default: + } + close(release) + <-first + <-second + }) +} + +func TestEvictionCallbackRequestsCloseAsynchronously(t *testing.T) { + for _, alreadyClosing := range []bool{false, true} { + name := "callback starts close" + if alreadyClosing { + name = "another caller already closing" + } + t.Run(name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + entered := make(chan struct{}) + requestClose := make(chan struct{}) + requested := make(chan struct{}) + returnAllowed := make(chan struct{}) + callbackCloseDone := make(chan struct{}) + var cache *Cache + config := DefaultConfig() + config.CleanupInterval = time.Second + config.OnEviction = func(string, any) { + close(entered) + <-requestClose + go func() { _ = cache.Close(); close(callbackCloseDone) }() + close(requested) + <-returnAllowed + } + cache = New(config) + cache.SetWithTTL(t.Context(), "expired", "value", -time.Second) + <-entered + otherCloseDone := make(chan struct{}) + if alreadyClosing { + go func() { _ = cache.Close(); close(otherCloseDone) }() + <-cache.stopChan + } + close(requestClose) + <-requested + synctest.Wait() + select { + case <-callbackCloseDone: + t.Error("Close returned before the eviction callback finished") + default: + } + close(returnAllowed) + <-callbackCloseDone + if alreadyClosing { + <-otherCloseDone + } + }) + }) + } +}