Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sink/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (s *Stats) Close() {
s.Shutdown(nil)
s.dataMsgRate.Stop()
s.undoMsgRate.Stop()
s.progressBlockRate.Stop()
}

type unsetBlockRef struct{}
Expand Down
37 changes: 37 additions & 0 deletions sink/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sink

import (
"runtime"
"testing"
"time"
)

// TestStatsCloseStopsAllRateJanitors verifies that Stats.Close() stops all
// three dmetrics rate objects (dataMsgRate, undoMsgRate, progressBlockRate).
//
// Each AvgRate* object spawns an internal janitor goroutine on construction.
// Before this fix, progressBlockRate.Stop() was missing, leaking one goroutine
// per Stats lifecycle. In sink.Sinker's continuous-mode usage this produces a
// linear goroutine slope over time.
func TestStatsCloseStopsAllRateJanitors(t *testing.T) {
runtime.GC()
time.Sleep(50 * time.Millisecond)
before := runtime.NumGoroutine()

const iterations = 50
for range iterations {
s := newStats(zlog)
s.Close()
}

runtime.GC()
time.Sleep(100 * time.Millisecond)
after := runtime.NumGoroutine()

delta := after - before
if delta > 2 {
t.Errorf("goroutine leak: started=%d ended=%d delta=%d (want ≤2); "+
"likely progressBlockRate.Stop() is not called in Stats.Close()",
before, after, delta)
}
}
Loading