mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2743] [Issue]: Provider streaming readers lack abort wiring & reader.cancel(); bodyTimeout:0 lets a stalled stream hang forever #1385
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1385
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2743
Originally assigned to: @SpicyMarinara on GitHub.
Summary
The OpenAI
chat()and Responses tool path registeroptions.signal -> reader.cancel()to proactively tear down the backend TCP connection on abort, butchatComplete()'s streaming branch andchatResponses()omit this — they rely solely on undici's fetch abort. ThecapStreamingResponsewrapperReadableStreamdefines onlystart()and nocancel(), so cancelling the wrapped body does not cancel the inner reader or close the per-request undici Agent. Finally, the LLM agent setsbodyTimeout: 0with only aheadersTimeout, so a stream that stalls mid-body (proxy half-open, model deadlock, token rate -> 0) has no inactivity timeout and the read loop awaits forever absent an external abort.Sub-bug 1: OpenAI chatComplete() streaming reader has no abort wiring (MEDIUM, leak)
chatComplete()stream branch grabsresponse.body.getReader()and loops with nosignal.addEventListener('abort', () => reader.cancel())and no in-loopsignal.abortedcheck; the signal only reachesfetch. On a tool-enabled abort, the reader is not proactively cancelled andonTokencan keep firing briefly (dropped at the route, which masks the symptom). Contrastchat()(978-990).openai.provider.ts:1230(reader),1246-1358(loop, no abort), vschat()983-990.Sub-bug 2: Responses chatResponses() has no abort wiring (LOW, abort-failure)
while(true)with no abort listener and nosignal.abortedcheck — the only abort wiring on the streaming Responses path is missing. Fetch-signal abort still unwinds the loop, so this is a missing redundant safety net rather than a hard hang.openai.provider.ts:1707-1812; contrastchat()983-990andchatCompleteResponses()1899-1906.Sub-bug 3: capStreamingResponse has no cancel() handler (LOW, leak)
new ReadableStream({ async start(controller){...} })defines onlystart(). The defaultcancelalgorithm is a no-op, so calling.cancel()on the wrapped body never cancels the inner source reader and neverdispatcher?.close()s the per-request Agent. Today the fetch signal masks this (signal abort rejects the inner read -> catch closes the dispatcher), so only a future caller cancelling without a backing signal leaks.security.ts:430-469(wrapper, no cancel),549-583(per-request Agent),base-provider.ts:20-40(llmFetch, bufferResponse:false).Sub-bug 4: bodyTimeout:0 — stalled body hangs forever (MEDIUM, hang)
llmAgentOptions = { bodyTimeout: 0, headersTimeout: 5min }.headersTimeoutonly bounds time-to-headers;bodyTimeout:0disables the inter-chunk inactivity timeout for the whole body. After headers + first byte, a stalled-but-open socket makesawait reader.read()block indefinitely; the provider loops have no per-read watchdog. The main route passes a signal but it only fires on client disconnect, so a stall while the client stays connected freezes the stream with no error/timeout. An internal caller without a signal would hang until process restart.base-provider.ts:13-14(options),36(default agentOptions); read loopsopenai.provider.ts:998-1004,anthropic.provider.ts:543-549,security.ts:441.Suggested Fix
chat()abort pattern tochatComplete()'s streaming branch andchatResponses(): bail early ifoptions.signal?.aborted, registerconst onAbort = () => reader.cancel().catch(()=>{})({once:true}), remove it in afinally, and optionallyif (options.signal?.aborted) break;at the top of the read loop.cancel(reason)method to thecapStreamingResponseReadableStream thatawait innerReader.cancel(reason)andawait dispatcher?.close().bodyTimeout: 0with a finite inter-chunk inactivity window (e.g. 60-120s) so a stalled body rejects withBodyTimeoutError, OR add a per-read inactivity watchdog that aborts when no bytes arrive within N seconds. (bodyTimeout is inter-chunk, not total duration, so a finite value won't kill a slow-but-healthy long stream.)Root-cause throughline
Proactive abort/cancel was wired into the original
chat()reader as defense-in-depth against undici not propagating aborts, but the laterchatComplete/Responses readers and thecapStreamingResponsewrapper never got the samereader.cancel()/cancel()plumbing, and the transport was tuned to never time out a body — so the system depends entirely on a single fetch-signal abort path and has no inactivity safety net for a mid-body stall.Verification checklist (leave all UNCHECKED)
pnpm checkAffected files
packages/server/src/services/llm/providers/openai.provider.tspackages/server/src/utils/security.tspackages/server/src/services/llm/base-provider.tsAudit provenance
Found by a multi-agent static audit of the streaming/SSE subsystem (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged
stagingtree. Severity: MEDIUM.@SpicyMarinara commented on GitHub (Jun 21, 2026):
Addressed on PR #2747. OpenAI chatComplete streaming and Responses chat streaming now wire options.signal to reader.cancel() and remove abort listeners in finally; the existing Responses chatComplete listener cleanup was also made exception-safe. capStreamingResponse now implements cancel(reason) that cancels the inner reader and closes the dispatcher. The shared LLM dispatcher and embedding/local-sidecar requests now use finite body timeouts instead of bodyTimeout: 0, preventing half-open body stalls from hanging forever. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.