Repository navigation
fix(sse): refactor stall detection to reduce false positives on slow streams - #4456
Merged
diegosouzapw merged 1 commit intoJun 20, 2026
Merged
Conversation
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
…but progressing streams
The stream stall watchdog now tracks raw upstream byte activity instead of
post-transform output. Reasoning models (Claude thinking via Kiro,
EventStream binary frames) can stream raw bytes for long stretches while
the SSE transform produces zero output as it accumulates a frame
boundary, which made the prior output-side watchdog false-stall on those
streams ("failed to pipe response" / Next.js disconnect).
`pipeWithDisconnect` now inserts an inert `TransformStream` tap between
the provider body and the SSE transform that resets the stall timer on
every upstream chunk. A wrapped controller clears the timer on every
termination path (complete / error / disconnect / abort) so a stale
abort cannot fire after the request has ended; on stall fire, the
watchdog errors the pipeline so the downstream reader unblocks and
surfaces a sanitized SSE error event (`buildStreamErrorChunks`).
The watchdog inherits `STREAM_IDLE_TIMEOUT_MS` as its default budget so a
single env knob still governs upstream silence tolerance; tests inject
an explicit budget via the new `opts.stallTimeoutMs` arg.
Validated by 3 new TDD tests in tests/unit/stream-handler.test.ts:
slow-but-progressing upstream is NOT flagged as stalled; truly silent
upstream IS flagged within budget; the timer is cleared on normal
completion so no late stall error surfaces.
Ported from decolua/9router#1243
Co-authored-by: Muhammad Zakir Ramadhan <zakirkun@users.noreply.github.com>
diegosouzapw
force-pushed
the
feat/port-pr-1243-stream-stall-detection
branch
from
June 20, 2026 23:26
aaa6a72 to
96a68aa
Compare
tkgo11
pushed a commit
to tkgo11/OmniRoute
that referenced
this pull request
Sep 23, 2026
…but progressing streams (diegosouzapw#4456) Integrated into release/v3.8.32
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
Ported from upstream PR
decolua/9router#1243by @zakirkun. Refactors stream stall detection inpipeWithDisconnectto track raw upstream byte activity instead of post-transform output, preventing false stalls on slow-but-progressing streams (reasoning models like Claude thinking via Kiro, EventStream binary frames buffering partial frames).Why
The prior watchdog (in OmniRoute's
createSSEStream/buildSseTransformStream) reset its idle timer only when post-transform output flowed. Reasoning streams emit many raw upstream bytes per single emitted SSE event — so the watchdog could false-fire even while upstream was actively progressing, surfacing a confusingfailed to pipe response/ disconnect to the client.The new layer in
pipeWithDisconnect:TransformStreamtap between the provider body and the SSE transform that resets the stall timer on every upstream chunk;buildStreamErrorChunkspath), thenabort()s the underlying fetch so upstream releases the connection;STREAM_IDLE_TIMEOUT_MSas its default budget (single env knob still governs upstream silence tolerance); tests inject an explicit budget via the newopts.stallTimeoutMsarg.TDD
Three new tests in
tests/unit/stream-handler.test.ts:onErrorcallback receives the stall, downstream gets a sanitized SSE error chunk withfinish_reason:"error".RED → GREEN confirmed (test #2 timed out at 5s pre-fix; passes in ~85ms post-fix). All 18 existing tests in the file still pass.
Test plan
node --test tests/unit/stream-handler.test.ts— 18/18 greennode --test tests/unit/stream-utilities.test.ts— 8/8 green (sibling pipeWithDisconnect coverage)npm run typecheck:core— cleanstreamHandler.ts532 < cap 800)Notes
open-sse/utils/streamHandler.ts409 → 532 (well below the 800-line new-file cap; not in the frozen baseline so no rebaseline needed).STREAM_IDLE_TIMEOUT_MSwatchdog inopen-sse/utils/stream.tsis intentionally left in place — it is a complementary check at a different layer (SSE transform output). The new watchdog is upstream-byte-side, exactly the false-positive fix the upstream PR targets.opts.stallTimeoutMs: 0opt-out preserves legacy behavior verbatim for any caller that needs to disable the watchdog.