-
Notifications
You must be signed in to change notification settings - Fork 13
fix(logger): nil out completed cancel entries in activeCancels #646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -211,11 +211,21 @@ func (l *FunLogger) Loading(format string, a ...any) context.CancelCauseFunc { | |||||||||||||||||||||||||||||||||||||||||||||
| return cancel | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| l.Wg.Add(1) | ||||||||||||||||||||||||||||||||||||||||||||||
| idx := len(l.activeCancels) | ||||||||||||||||||||||||||||||||||||||||||||||
| l.activeCancels = append(l.activeCancels, cancel) | ||||||||||||||||||||||||||||||||||||||||||||||
| l.mu.Unlock() | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| go l.runLoading(ctx, fmt.Sprintf(format, a...)) | ||||||||||||||||||||||||||||||||||||||||||||||
| return cancel | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Return a wrapper that cancels and marks the slot as nil | ||||||||||||||||||||||||||||||||||||||||||||||
| return func(cause error) { | ||||||||||||||||||||||||||||||||||||||||||||||
| cancel(cause) | ||||||||||||||||||||||||||||||||||||||||||||||
| l.mu.Lock() | ||||||||||||||||||||||||||||||||||||||||||||||
| if idx < len(l.activeCancels) { | ||||||||||||||||||||||||||||||||||||||||||||||
| l.activeCancels[idx] = nil | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+224
to
+225
|
||||||||||||||||||||||||||||||||||||||||||||||
| if idx < len(l.activeCancels) { | |
| l.activeCancels[idx] = nil | |
| if idx < len(l.activeCancels) { | |
| // Mark this slot as inactive | |
| l.activeCancels[idx] = nil | |
| // Compact trailing nil entries so the slice length (and eventually | |
| // its backing array) does not grow without bound across many | |
| // transient loading operations. | |
| for len(l.activeCancels) > 0 { | |
| lastIdx := len(l.activeCancels) - 1 | |
| if l.activeCancels[lastIdx] != nil { | |
| break | |
| } | |
| l.activeCancels = l.activeCancels[:lastIdx] | |
| } | |
| // When all entries are inactive, reset the slice so the backing | |
| // array can be garbage-collected and capacity reset. | |
| if len(l.activeCancels) == 0 { | |
| l.activeCancels = nil | |
| } |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new behavior of nil-ing out cancel function entries lacks test coverage. While existing tests verify that Loading works correctly, there are no tests that verify:
- The cancel function slot is actually set to nil after calling the returned wrapper
- Multiple sequential Loading calls don't leak memory via the slice
- Calling the wrapper after Exit() doesn't cause issues
Consider adding a test that verifies the nil-ing behavior and slice state after cancel is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a potential race condition if the wrapper cancel function is called after
Exit()has setl.activeCancels = nil. The bounds checkif idx < len(l.activeCancels)will correctly prevent a panic (since len(nil) is 0), but this is a subtle correctness property that may not be immediately obvious to future maintainers.Consider adding a comment explaining this safety property, or explicitly check for nil:
This makes the intention clearer and is more defensive against future refactoring.