fix (core): consume stream on abort#5492
Conversation
examples/next-openai/app/api/use-chat-resilient-persistence/route.ts
Outdated
Show resolved
Hide resolved
|
Needs a changeset. |
|
How does this affect the useChat UI parts? |
Before this change, because More details here |
So consume stream would break an ongoing response? (asking because at that point consume stream should run async and separately of the function) |
Ah aligned, consumeStream = independent from function! I misspoke (corrected above), and the unhandled error does/should not directly impact useChat. Yesterday, while repro-ing, the unhandled error from consumeStream seemed to put the server in a funky state - verifying now but upon the subsequent user message, useChat's |
|
The consumeStream resilience is very good regardless (and maybe it should swallow all errors or have an error callback to not break any server). |
|
@lgrammel heard! Also, I'm not crazy, I did see status set to CleanShot.2025-04-02.at.11.49.17.mp4If |
|
@iteratetograceness that is very interesting - do you know what happens (on the network level)? is the next js server forwarding the error through the stream somehow? other server might behave differently |
I don't think this is Next.js server specific! I believe the unhandled network error is bubbling up to the nearest error handler in the event loop (which prior to these changes would be triggerRequest's catch) |
|
@lgrammel oh but I am seeing a slightly different edge case now! If a subsequent message isn't sent after the stopping, onFinish does not trigger. Any inklings on what causes this? I'm gonna take a closer look at the stream logic in stream-text.ts |
|
@lgrammel tldr, when the abort signal is passed into the streamText, consumeStream does not trigger onFinish (the onFinish callback is only called as a side effect of subsequent messages' flushes) im stepping away for a bit but will come back to dig deeper and flesh out the appropriate solution (e.g. maybe something along the lines of adding new |
| If an error occurs, it is passed to the optional `onError` callback. | ||
| */ | ||
| consumeStream(): Promise<void>; | ||
| consumeStream(onError?: (error: unknown) => void): Promise<void>; |
|
is it released already? I am still experiencing the bug, even after upgrading to |
|
@ItayElgazar this fix has already been released. I recommend opening a new discussion/issue with more context or minimal repro steps! |
Addresses #5459
This PR ensures that the
consumeStreammethod onstreamTextdoes not throw unhandled errors.NOTE: This is an edge case that occurs when an
abortSignalis passed tostreamText; the aborted signal propagates toconsumeStreamas aResponseAbortederror but we were not catching this error. This then triggered a network error, caught by theuseChathook (setting the hook status toerror).