mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2739] [Issue]: Streaming abort handling — Stop persists a truncated assistant message + aborts surface as errors + mid-stream partial discarded (non-tool path) #1379
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#1379
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/2739
Originally assigned to: @SpicyMarinara on GitHub.
Summary
The non-tool streaming path in
generate.routes.tsis asymmetric with the tool path: the tool path returnsnullon abort before the shared save/emit block, but the streamingelsebranch has no abort guard between the stream loop and the DB save. As a result, pressing Stop (or a qualifying disconnect) mid-generation persists the truncated text as a real assistant message, and an abort that surfaces as a rejectedreader.read()is reported to the client as a genericerrorSSE event. A related mid-stream failure path discards already-streamed content on both the client and in the basechatComplete()collector.Sub-bug 1: Abort saves a truncated assistant message (HIGH, data-loss)
reader.cancel()resolves the next read with{done:true}, so the stream loop ends with a partial-but-nonemptyfullResponse. There is noif (abortController.signal.aborted) return nullbetween the loop end and the save, so execution reachescontent_replace,chats.createMessage({content: fullResponse}), andmessage_saved. On reload the user sees the reply they thought they cancelled. The empty-response guardif (!fullResponse.trim())does not fire for a partial buffer.generate.routes.ts:6066,6113-6114,6120-6121. Non-tool branch consumes the generator at6325-6337with no abort check; reachescontent_replace(6364,6609),chats.createMessage/addSwipe(6664-6672),message_saved(6751). Client Stop reaches abort viachat.store.ts:346-358->/api/generate/abort->generate.routes.ts:10700-10709.Sub-bug 2: Spurious error event on abort (LOW, wrong-output)
reader.read()reject with AbortError, it propagates out ofgenerateForCharacter(onlyfinallyat6855, no catch) to the outer catch at10667, which unconditionally callssendSseEvent(reply, {type:"error", data: message})at10677with nosignal.abortedshort-circuit. (Note: in practice the client aborts its own controller first and/orclientDisconnectedsuppresses the write, so the toast is usually not observed — this is primarily an asymmetry/code-smell vs the tool path.)6111-6117guards) vs outer catch (10667-10677no guard).generate.routes.ts:6290-6340(no try/catch),10667-10677(unconditional error emit).Sub-bug 3: Client drops visibly-streamed partial on mid-stream error (MEDIUM, data-loss)
{type:'error'}after some tokens streamed, the client error handler flushes the typewriter (still only instreamBuffers, not persisted),streamEventsreturns, and thefinallyrunsclearStreamBufferthenrefreshMessagesAuthoritativelywhich overwrites the view from the DB. If the server saved no partial row, the streamed text disappears.use-generate.ts:2170-2178(error handler),2307-2323(finally clearStreamBuffer + refresh); server emits error without partial save atgenerate.routes.ts:10667-10677.Sub-bug 4: base chatComplete() discards partial on mid-stream throw (MEDIUM, data-loss)
content += result.value; result = await gen.next()has no try/catch. Whengen.next()rejects mid-stream (Anthropic/Google provider throwing on an error event, or undici body drop), the accumulatedcontentis thrown away and the caller (summarizers, trackers, selectors with noonToken) gets an exception and empty result.chatCompletecall (e.g. auto-summary); the salvageable partial is lost.base-provider.ts:613-627(no try/catch); Anthropic throws atanthropic.provider.ts:571-573(try at 542 has only finally at 606); Google atgoogle.provider.ts:741/764.Suggested Fix
if (abortController.signal.aborted) return null;immediately after the non-tool stream loop (~generate.routes.ts:6340), and wrapgen.next()so an AbortError is treated as a silent cancel returning null (mirror the tool path at 6111-6117).10667), short-circuit aborts:if (abortController.signal.aborted || err?.name === 'AbortError') return;before emittingerror.receivedContentis true and nomessage_savedarrived, persist/keep the streamedfullBufferas a local partial rather than clearing+refetching over it (or have the server persist the partial before emitting error).chatComplete()consumption loop in try/catch and return the partialcontent(withfinishReason: 'error'/'length') instead of discarding it.Root-cause throughline
The tool-enabled path was hardened to treat abort and mid-stream failure as graceful, salvageable terminations, but the default non-tool streaming path and the base collector were never brought to parity — they assume a clean run-to-completion and fall straight through to save/emit, so an abort or mid-stream throw becomes either a persisted truncated message, a spurious error toast, or a discarded partial.
Verification checklist (leave all UNCHECKED)
pnpm checkAffected files
packages/server/src/routes/generate.routes.tspackages/server/src/services/llm/base-provider.tspackages/client/src/hooks/use-generate.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: HIGH.@SpicyMarinara commented on GitHub (Jun 21, 2026):
Addressed on PR #2747. Non-tool streaming now treats AbortError as silent cancellation, checks the abort signal before post-processing/saving, suppresses abort error SSEs in the outer catch, keeps visible client partials when an error arrives before message_saved, and the base chatComplete() collector returns partial content on mid-stream failure instead of discarding it.