From a6cffd0a6d11ef8aa3a0ca0ddc15879d0845605d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 23 Oct 2026 18:05:59 +0000 Subject: [PATCH] drive/driveimpl/compositedav: don't run ttlcache cleanup goroutines StatCache started each ttlcache with "go cache.Start()" and stopped them in StatCache.stop. But ttlcache caches begin in the stopped state, so if stop ran before the Start goroutine was scheduled, Stop was a no-op and Start then ran forever, leaking a goroutine. That made tests using tstest.ResourceCheck flaky: goroutine github.com/jellydator/ttlcache/v3.(*Cache[...]).Start Rather than synchronize with Start, drop the background goroutine entirely. Cache.Get already ignores expired items, so the goroutine only existed to free memory. Instead, call DeleteExpired on each write, which is cheap: it pops expired entries from the front of the expiration heap and stops at the first unexpired one. With no goroutines, StatCache.stop is no longer needed, so remove it. Fixes #21824 Updates tailscale/corp#49459 Change-Id: Ib438fcd45296be79528613c5f5687725c38a137d Signed-off-by: Brad Fitzpatrick --- drive/driveimpl/compositedav/compositedav.go | 7 +------ drive/driveimpl/compositedav/stat_cache.go | 17 +++++++---------- drive/driveimpl/compositedav/stat_cache_test.go | 4 ---- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/drive/driveimpl/compositedav/compositedav.go b/drive/driveimpl/compositedav/compositedav.go index c6ec79772..d4edbc523 100644 --- a/drive/driveimpl/compositedav/compositedav.go +++ b/drive/driveimpl/compositedav/compositedav.go @@ -222,8 +222,7 @@ func (h *Handler) GetChild(name string) *Child { return child } -// Close closes this Handler,including closing all idle connections on children -// and stopping the StatCache (if caching is enabled). +// Close closes this Handler, including closing all idle connections on children. func (h *Handler) Close() { h.childrenMu.RLock() oldChildren := h.children @@ -233,10 +232,6 @@ func (h *Handler) Close() { for _, child := range oldChildren { child.CloseIdleConnections() } - - if h.StatCache != nil { - h.StatCache.stop() - } } func (h *Handler) findChildLocked(name string) (int, *Child) { diff --git a/drive/driveimpl/compositedav/stat_cache.go b/drive/driveimpl/compositedav/stat_cache.go index 176a09d84..82d445315 100644 --- a/drive/driveimpl/compositedav/stat_cache.go +++ b/drive/driveimpl/compositedav/stat_cache.go @@ -216,9 +216,15 @@ func (c *StatCache) setLocked(name string, depth int, ce *cacheEntry) { cache = ttlcache.New( ttlcache.WithTTL[string, *cacheEntry](c.TTL), ) - go cache.Start() c.cachesByDepthAndPath[depth] = cache } + // Rather than running ttlcache's background cleanup goroutine (which + // would need to be started and stopped, and which races if Stop is + // called before Start runs), evict expired entries opportunistically + // on each write. Get already ignores expired entries, so this only + // bounds memory. DeleteExpired is cheap: it pops from the front of the + // expiration heap and stops at the first unexpired entry. + cache.DeleteExpired() cache.Set(name, ce, ttlcache.DefaultTTL) } @@ -236,15 +242,6 @@ func (c *StatCache) invalidate() { } } -func (c *StatCache) stop() { - c.mu.Lock() - defer c.mu.Unlock() - - for _, cache := range c.cachesByDepthAndPath { - cache.Stop() - } -} - type cacheEntry struct { Status int Raw []byte diff --git a/drive/driveimpl/compositedav/stat_cache_test.go b/drive/driveimpl/compositedav/stat_cache_test.go index 9be286389..2d5242b9c 100644 --- a/drive/driveimpl/compositedav/stat_cache_test.go +++ b/drive/driveimpl/compositedav/stat_cache_test.go @@ -70,7 +70,6 @@ func TestStatCacheNoTimeout(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 5 * time.Second} - defer c.stop() // check get before set fetched := c.get(childPath, 0) @@ -98,7 +97,6 @@ func TestStatCacheTimeout(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 250 * time.Millisecond} - defer c.stop() // set new stat ce := newCacheEntry(http.StatusMultiStatus, fullChild) @@ -130,7 +128,6 @@ func TestParentChildRelationship(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 24 * time.Hour} // don't expire - defer c.stop() missingParentPath := "/missingparent" unparseableParentPath := "/unparseable" @@ -224,7 +221,6 @@ func TestUnicodeNormalizationInsensitivity(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 24 * time.Hour} // don't expire - defer c.stop() const ( nfcParent = "\u30ae\u30bf\u30fc" // ギター in NFC: ギ is the single code point U+30AE