Skip to content

Commit 7421b7d

Browse files
fix(logger): nil out completed cancel entries in activeCancels (#646)
activeCancels grew unbounded until Exit(). Now each cancel wrapper nils its slot so the reference is released. Exit() still iterates all entries for safety, skipping nil entries. Audit finding #30 (LOW). Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
1 parent 38c2b4b commit 7421b7d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

internal/logger/logger.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,21 @@ func (l *FunLogger) Loading(format string, a ...any) context.CancelCauseFunc {
211211
return cancel
212212
}
213213
l.Wg.Add(1)
214+
idx := len(l.activeCancels)
214215
l.activeCancels = append(l.activeCancels, cancel)
215216
l.mu.Unlock()
216217

217218
go l.runLoading(ctx, fmt.Sprintf(format, a...))
218-
return cancel
219+
220+
// Return a wrapper that cancels and marks the slot as nil
221+
return func(cause error) {
222+
cancel(cause)
223+
l.mu.Lock()
224+
if idx < len(l.activeCancels) {
225+
l.activeCancels[idx] = nil
226+
}
227+
l.mu.Unlock()
228+
}
219229
}
220230

221231
func (l *FunLogger) runLoading(ctx context.Context, message string) {
@@ -277,7 +287,9 @@ func (l *FunLogger) Exit(code int) {
277287
l.mu.Lock()
278288
l.exited = true
279289
for _, cancel := range l.activeCancels {
280-
cancel(nil)
290+
if cancel != nil {
291+
cancel(nil)
292+
}
281293
}
282294
l.activeCancels = nil
283295
l.mu.Unlock()

0 commit comments

Comments
 (0)