From 6dc0bd834c0858332aff1579bc4559934f6d242c Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Mon, 12 Jan 2026 11:43:41 -0800 Subject: [PATCH] util/limiter: don't panic when dumping a new Limiter Fixes #18439 Signed-off-by: Josh Bleecher Snyder --- util/limiter/limiter.go | 3 +++ util/limiter/limiter_test.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/util/limiter/limiter.go b/util/limiter/limiter.go index b86efdf29..b5fbb6fa6 100644 --- a/util/limiter/limiter.go +++ b/util/limiter/limiter.go @@ -187,6 +187,9 @@ func (lm *Limiter[K]) collectDump(now time.Time) []dumpEntry[K] { lm.mu.Lock() defer lm.mu.Unlock() + if lm.cache == nil { + return nil + } ret := make([]dumpEntry[K], 0, lm.cache.Len()) lm.cache.ForEach(func(k K, v *bucket) { lm.updateBucketLocked(v, now) // so stats are accurate diff --git a/util/limiter/limiter_test.go b/util/limiter/limiter_test.go index 77b1d562b..d3f3e307a 100644 --- a/util/limiter/limiter_test.go +++ b/util/limiter/limiter_test.go @@ -5,6 +5,7 @@ package limiter import ( "bytes" + "io" "strings" "testing" "time" @@ -175,6 +176,10 @@ func TestDumpHTML(t *testing.T) { } } +func TestDumpHTMLEmpty(t *testing.T) { + new(Limiter[string]).DumpHTML(io.Discard, false) // should not panic +} + func allowed(t *testing.T, limiter *Limiter[string], key string, count int, now time.Time) { t.Helper() for i := range count {