Repository navigation
Event streaming (9/11): Print each event without waiting for the next heartbeat - #15079
Merged
Merged
Conversation
…tbeat The stream was sanitized with a transform.Reader before framing. In JSON mode the sanitizer holds back the last bytes of its input until more arrive, so the end of each frame stayed buffered until the next keep-alive, delaying events by up to a heartbeat interval. Frame the raw stream and sanitize each frame's data as a complete input instead. An event that is not valid UTF-8 is now skipped with a warning rather than failing the stream. Co-authored-by: Copilot App <[email protected]> Co-authored-by: Ian C. Anderson <[email protected]>
babakks
requested review from
niik
and
a balanced review from Copilot
and removed request for
a team
November 17, 2026 17:11
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Tests do not cover end-frame reason sanitization or verify that invalid UTF-8 produces an "invalid end of stream frame" error rather than being skipped.
1 open issue
What changed in this PR
This ninth layer of the Event Subscriptions stack fixes delayed output in gh event listen by sanitizing complete frame data rather than the incoming stream.
Changes:
- Prints completed events without waiting for the next heartbeat.
- Preserves data sanitization while skipping ordinary events with invalid UTF-8 and rejecting invalid end frames.
- Adds regression tests for immediate delivery on an open stream and per-frame sanitization.
| File | Description |
|---|---|
pkg/cmd/event/listen/listen.go |
Moves sanitization after framing and handles sanitization errors by frame type. |
pkg/cmd/event/listen/listen_test.go |
Tests delivery without further input, control-character sanitization, and invalid UTF-8 recovery. |
Review effort: Balanced
2 of 6 tasks
2 of 6 tasks
BagToad
approved these changes
Nov 17, 2026
Merged
3 tasks
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.

Layer 9 of 11 in the stack tracked by #15070.
Description
gh event listenreads a server-sent events stream, where each event arrives as a frame that ends with a blank line, and the server sends a: keep-alivecomment about every 9 seconds. The command prints an event once the frame reader sees the blank line that ends it.Before framing,
readEventspassed the stream through go-gh'sasciisanitizer.Sanitizer{JSON: true}, which replaces terminal control characters. In JSON mode, the sanitizer needs 6 bytes to recognize an escaped character such as\^[, so it holds back the last bytes of its input until more bytes arrive or the stream ends. The end of each frame (}}\n\n) stayed buffered until the next keep-alive arrived, so every event printed one heartbeat late. With the default "exit after the first event" behavior, the command also exited late. The unit tests missed this because each mocked body ended with EOF, and EOF flushes the buffer.This change splits the raw stream into frames first, then sanitizes each frame's data as a complete string, so no bytes are held back. The output is sanitized the same way as before: raw and JSON-escaped C0/C1 control characters are neutralized, and escape sequences that
--templatehelpers such ascolorproduce are kept.How did you test this change?
I built
bin/ghfrom this branch, and a second binary frombabakks/event-listen(#15072), which has the bug. Against github.com, I rangh event listen --resource <PR URL> --event com.github.pull_request.state.changed.v1 --timeout 90on a test pull request. Once the command printed "Listening for events.", I closed or reopened the pull request. I measured the time from that action to the event line on stdout, alternating between the two binaries:The times for this branch are close to the ~4s at which a raw
curl -Nof the events endpoint received the frame. Each time on the base branch is the delivery time plus the wait for the next keep-alive. Both binaries exited with status 0 after printing the event.I also ran the two new tests against the base branch's
listen.goand saw both fail. The pipe test timed out because the event was never printed, and the sanitization test failed withinvalid UTF-8 string.Key points
reasonstays sanitized where it appears in stderr messages.Event,IDand comments are only compared or ignored, never printed, so they aren't sanitized.warning: skipped an event that is not valid UTF-8, the same way an event that isn't valid JSON is handled. An end frame that isn't valid UTF-8 returns the existinginvalid end of stream frameerror.json.Compactinstead. Sanitizing the frame data keeps the existing order (sanitize, then parse) and works the same for--jqand--template.Notes for reviewers
readEventsinpkg/cmd/event/listen/listen.go, then the two new tests at the end oflisten_test.go.TestReadEventsPrintsEventBeforeMoreBytesArriveuses anio.Pipethat writes one frame and then blocks, the case the existing tests didn't cover.gh event listen#15072. The fix is its own layer so it is easy to review on its own; it could also be folded into Event streaming (2/11): Addgh event listen#15072 before merging.make lintdidn't run locally: the installed golangci-lint was built with go1.26, and the module targets go1.27.3.Authorship and follow-up
Who wrote this:
Who answers review comments: