fix(output): make rendering cancellable mid-stream - #26
Merged
Merged
Conversation
14 tasks
hammadmajid
force-pushed
the
fix/17-output-cancellation
branch
from
September 5, 2026 14:14
c43c443 to
a7037ae
Compare
Formatter.Format took no context, so after emitResults' single pre-flight ctx.Err() check a multi-million-line result set streamed to stdout with no way to stop it. If stdout was a slow pipe, Ctrl-C was inert for the entire render. Add a leading ctx to the Formatter interface and thread it through all four concrete formatters (grouped, single-line, count, files-with-matches). Each consults the context through a cancelGuard, which reads it at file-group boundaries and once every 1024 emitted lines; the per-line cost is a decrement and a branch, keeping the atomic load off the innermost write path. Checks sit between line writes, so the output produced before an abort is always a whole-line prefix of the complete rendering. In emitResults the now-redundant pre-flight check is folded into the render itself, and a cancellation return maps to cancelError using the same predicate as the walker call site above it. Also updates test/leak_test.go's single Format call site to the new signature (agreed with the lead; no agent owns that file in this wave). Verified end-to-end: rendering 34962 lines into a slow pipe, SIGINT is ignored for >5s before this change and aborts after 0.32s with only 285 lines emitted after it. Closes #17
hammadmajid
force-pushed
the
fix/17-output-cancellation
branch
from
September 5, 2026 14:16
a7037ae to
e485a09
Compare
hammadmajid
marked this pull request as ready for review
September 5, 2026 14:20
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.
Description
After a single pre-flight check, a multi-million-line result set streamed to stdout with no cancellation. Into a slow pipe, Ctrl-C was inert for the whole render.
Closes #17
Type of Change
Key Changes
Formatter.Formatgained a leadingctx context.Context, threaded into all four concrete formatters (GroupedFormatter,SingleLineFormatter,CountFormatter,FilesWithMatchesFormatter).An unexported
cancelGuardholds the context plus a plainintline budget.boundary()forces actx.Err()read at each file-group boundary and restarts the interval;lines(n)decrements the budget and only reads the context once every 1024 lines. Per-line cost is a decrement and a branch, so the atomic load stays off the innermost write path. A nil context degrades tocontext.Background().Guard checks sit between line writes, so whatever was emitted before an abort is always a whole-line prefix of the complete render — never a truncated line.
In
emitResultsthe now-redundant pre-flight check is folded into the render, and a cancellation return maps tocancelErrorusing the same predicate already used at thewalker.Walkcall site.Verification & Testing
go test -v -count=1 ./...go test -race -shuffle=on -count=1 ./...go vet ./...End-to-end proof the fix is load-bearing. 34,962-line render into a deliberately slow pipe (~500 lines/sec consumer), SIGINT after 1 s:
HEAD)The 0.3 s is the blocked
write(2)draining the 64 KB pipe buffer, after which the guard observes the cancellation.New tests.
TestFormatterCancelledMidRendertables over all four formatters, asserts the fixture spans more than two check intervals, then re-renders through a writer that cancels after 10 emitted lines — assertingerrors.Is(err, context.Canceled), non-empty output, strictly shorter than the full render, astrings.HasPrefixof it, and newline-terminated.TestFormatterAlreadyCancelledEmitsNothingdefends the folded pre-flight check.Mutation: neutering the guard's
ctx.Err()reads fails all 5 subtests. Separately, mutatingCountFormatterto observe cancellation but still emit everything fails oncancellation emitted 78000 of 78000 bytes: the remainder was not skipped— proving the strict-prefix and length assertions have teeth independently of the error assertion.Note
Stacked on #25.
Checklist
gofmtclean