Skip to content

fix(web): bound SSE writes so a stalled client cannot block the run - #487

Merged
umputun merged 1 commit into
umputun:masterfrom
Master-Norna:fix/sse-write-deadline
Nov 26, 2026
Merged

umputun merged 1 commit into
umputun:masterfrom
Master-Norna:fix/sse-write-deadline

Conversation

@Master-Norna

@Master-Norna Master-Norna commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The dashboard streams progress over SSE. handleEvents (pkg/web/server.go:284) hands the response to go-sse's Joe provider, and Joe broadcasts synchronously: Session.Publish calls sub.Client.Send/Flush for every subscriber before it returns. A client that stops reading — a closed laptop lid, a frozen tab, a stalled tunnel — fills its socket buffer, and Publish blocks forever. Since BroadcastLogger.Print publishes every progress line, the whole run stalls behind that client, and StopTailing/RefreshStates block with it.

Reproduced against a real socket that sends the SSE request and never reads: with a 4 KiB receive buffer, the fourth 32 KiB event blocks Publish for more than three seconds; a goroutine dump shows Joe.start → Client.Send → net.(*conn).Write.

Fix

Wrap the response writer in handleEvents so every Write and Flush sets a 10 second deadline with http.ResponseController. A stalled subscriber now fails its own write; Joe drops that subscription on the send error and the publisher continues. The deadline is refreshed on every write, so a client that keeps reading is never disconnected, and the browser's EventSource reconnects with Last-Event-ID and replays from the existing FiniteReplayer.

The wrapper keeps the underlying writer's FlushError visible, so a failed flush still drops the subscriber the way go-sse did before the wrapper; a writer that supports neither http.Flusher nor FlushError is passed through untouched, so go-sse still rejects it with the same 500.

SetWriteDeadline is best-effort: a writer that does not support it (such as httptest.ResponseRecorder) keeps the previous behavior. pkg/web serves plain HTTP/1.1, so HTTP/2 is out of scope for this change.

Validation

  • TestServer_HandleEvents_SlowClientDoesNotBlockPublish opens a real socket, never reads it, and publishes 32 KiB events until handleEvents returns: on master the blocked Publish trips a 3 s guard; with the change the stalled subscriber is dropped at the write deadline and the following publishes stay fast.
  • TestServer_HandleEvents_ActiveClientStaysConnected keeps reading and idles longer than the deadline between events, so a deadline that is not refreshed is caught as a dropped subscriber.
  • TestSSEClientWriter_FlushErrorPropagates keeps the wrapper from hiding the underlying writer's FlushError, and TestServer_HandleEvents_NonFlusherWriter keeps the old 500 for a writer without flush support.
  • go test ./pkg/web: passed. go test ./...: the only failure is pkg/progress's TestNewLogger_HistoryStemCannotEscapeHistoryDirectory (Windows rejects the progress-.. history path); it fails the same way on master.
  • golangci-lint run --max-issues-per-linter=0 --max-same-issues=0 ./... (v2.13.0): 0 issues. gofmt -s, go vet: passed.

Agent Yan assisted participation

Joe broadcasts synchronously: Session.Publish writes to every subscriber
before it returns, so a client that stops reading its SSE stream blocks
BroadcastLogger.Print and with it the whole run. Set a per-write deadline
in handleEvents so a stalled subscriber fails its own connection instead;
the deadline is refreshed on every write, so an active client is never
disconnected, and EventSource reconnects with Last-Event-ID to replay.

Co-Authored-By: Agent-Yan <352840372+Agent-Yan@users.noreply.github.com>

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lgtm

@umputun
umputun merged commit 76607e0 into umputun:master Nov 26, 2026
2 checks passed
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.

2 participants