fix: exorcise goroutine-leaking util.After from the codebase#211
Merged
fix: exorcise goroutine-leaking util.After from the codebase#211
Conversation
util.After spawned a goroutine that sent on an unbuffered channel. If the caller's select picked a different branch (e.g. ctx.Done()), the goroutine blocked forever on the send — leaking one goroutine per cancellation. Replace all 5 call sites with inline clock.NewTimer + explicit Stop(), then delete util.After entirely so nobody can reintroduce the leak. Fixes #210
|
✅ Preview binaries are ready! To test with modules: |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes the goroutine-leaking util.After helper and replaces its usage with inline quartz.Clock.NewTimer timers that are explicitly Stop()’d, preventing goroutine leaks when a select takes a non-timer branch (e.g., ctx.Done()).
Changes:
- Deleted
util.Afterfromlib/util/util.go. - Replaced all previous
util.Aftercall sites withclock.NewTimer(...)+Stop()patterns (including ctx-cancel paths where applicable). - Cleaned up
cmd/attachimports after removing the lastutil.Afterusage.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/util/util.go | Removes the leaky After helper entirely. |
| lib/termexec/termexec.go | Replaces the vsync sleep and Close timeout with NewTimer and Stop(). |
| lib/screentracker/pty_conversation.go | Replaces Phase 1 stability + Phase 2 carriage-return delays with explicit timers that are stopped on both normal and cancel paths. |
| cmd/attach/attach.go | Replaces grace-period wait with NewTimer and removes now-unused util import. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
DanielleMaywood
approved these changes
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
util.Aftercall sites with inlineclock.NewTimer+ explicitStop()util.Afterfunction entirelyProblem
util.Afterspawned a goroutine that sent on an unbuffered channel. If the caller'sselectpicked a different branch (e.g.ctx.Done()), the goroutine blocked forever on the send — leaking one goroutine per cancellation.Changes
lib/screentracker/pty_conversation.go— Phase 1 stability timer + Phase 2 carriage-return timer now use inlineNewTimerwithStop()on both cancel and normal pathslib/termexec/termexec.go—ReadScreenvsync timer +Closetimeout timer replaced with inlineNewTimercmd/attach/attach.go— Grace period timer replaced, removed unusedutilimportlib/util/util.go— DeletedAfterfunctionImplementation context
The goroutine leak was originally flagged by Copilot in PR #208. The
Afterhelper usedmake(chan time.Time)(unbuffered) + a goroutine doingch <- <-timer.C. If nobody drainedch, the goroutine blocked on send forever. InlineNewTimeris the idiomaticquartzpattern already used byWaitForitself.Fixes #210