Skip to content

Event streaming (9/11): Print each event without waiting for the next heartbeat - #15079

Merged
BagToad merged 1 commit into
babakks/event-evalsfrom
babakks/event-listen-flush
Nov 17, 2026
Merged

BagToad merged 1 commit into
babakks/event-evalsfrom
babakks/event-listen-flush

Conversation

@babakks

@babakks babakks commented Nov 17, 2026

Copy link
Copy Markdown
Member

Layer 9 of 11 in the stack tracked by #15070.

Description

gh event listen reads a server-sent events stream, where each event arrives as a frame that ends with a blank line, and the server sends a : keep-alive comment about every 9 seconds. The command prints an event once the frame reader sees the blank line that ends it.

Before framing, readEvents passed the stream through go-gh's asciisanitizer.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 --template helpers such as color produce are kept.

How did you test this change?

I built bin/gh from this branch, and a second binary from babakks/event-listen (#15072), which has the bug. Against github.com, I ran gh event listen --resource <PR URL> --event com.github.pull_request.state.changed.v1 --timeout 90 on 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:

Binary Run 1 Run 2 Run 3
This branch 3.6s 4.1s 3.9s
Base branch 16.0s 16.0s 25.0s

The times for this branch are close to the ~4s at which a raw curl -N of 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.go and saw both fail. The pipe test timed out because the event was never printed, and the sanitization test failed with invalid UTF-8 string.

Key points

  • The end frame's data is sanitized too, so its reason stays sanitized where it appears in stderr messages. Event, ID and comments are only compared or ignored, never printed, so they aren't sanitized.
  • Behavior change: before, a frame that wasn't valid UTF-8 failed the whole stream ("Event stream failed, reconnecting"). Now such an event is skipped with 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 existing invalid end of stream frame error.
  • I considered sanitizing the output of json.Compact instead. Sanitizing the frame data keeps the existing order (sanitize, then parse) and works the same for --jq and --template.

Notes for reviewers

  • Start with readEvents in pkg/cmd/event/listen/listen.go, then the two new tests at the end of listen_test.go. TestReadEventsPrintsEventBeforeMoreBytesArrive uses an io.Pipe that writes one frame and then blocks, the case the existing tests didn't cover.
  • The bug comes from Event streaming (2/11): Add 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): Add gh event listen #15072 before merging.
  • @iancanderson wrote this fix, and the commit credits him as a co-author.
  • make lint didn'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:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @babakks will read and reply directly. Name the account.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

…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
babakks requested a review from a team as a code owner November 17, 2026 17:11
@babakks
babakks requested review from niik and a balanced review from Copilot and removed request for a team November 17, 2026 17:11
@babakks
babakks added this pull request to stack #15082 November 17, 2026 17:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread pkg/cmd/event/listen/listen.go Resolved
Copilot AI mentioned this pull request Nov 17, 2026
2 of 6 tasks
@babakks
babakks requested review from BagToad and removed request for niik November 17, 2026 17:16
@BagToad
BagToad merged commit 42d52b4 into trunk Nov 17, 2026
19 checks passed
@BagToad
BagToad deleted the babakks/event-listen-flush branch November 17, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants