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 <[email protected]>
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-10-23 20:41:40 -04:00
1 parent ae4ab7e7d5
commit a6cffd0a6d
3 files changed
+8 -20

No files matched your search

+1 -6
View File
@@ -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) {
+7 -10
View File
@@ -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
@@ -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