Skip to content

Commit a000a1d

Browse files
committed
fix(event,processors): Prevent slice capacity leak
Hard free the event buffer to prevent slice leak when the capacity grows and GC cannot reclaim the memory because the buffer still references it.
1 parent 2eab2d5 commit a000a1d

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

internal/etw/processors/fs_windows.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,17 @@ func (f *fsProcessor) purge() {
372372

373373
// evict unmatched stack traces
374374
for id, q := range f.buckets {
375-
s := q[:0]
375+
s := make([]*event.Event, 0, len(q))
376376
for _, evt := range q {
377377
if time.Since(evt.Timestamp) <= time.Second*30 {
378378
s = append(s, evt)
379379
}
380380
}
381-
f.buckets[id] = s
381+
if len(s) == 0 {
382+
delete(f.buckets, id)
383+
} else {
384+
f.buckets[id] = s
385+
}
382386
}
383387

384388
f.mu.Unlock()

pkg/event/stackwalk.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (s *StackwalkDecorator) flush() []error {
210210
errs := make([]error, 0)
211211

212212
for id, q := range s.buckets {
213-
n := q[:0]
213+
n := make([]*Event, 0, len(q))
214214
for _, evt := range q {
215215
if time.Since(evt.Timestamp) < maxQueueTTLPeriod {
216216
n = append(n, evt)
@@ -230,7 +230,11 @@ func (s *StackwalkDecorator) flush() []error {
230230
}
231231
stackwalkFlushesEvents.Add(evt.Name, 1)
232232
}
233-
s.buckets[id] = n
233+
if len(n) == 0 {
234+
delete(s.buckets, id)
235+
} else {
236+
s.buckets[id] = n
237+
}
234238
}
235239

236240
return errs

0 commit comments

Comments
 (0)