-
Notifications
You must be signed in to change notification settings - Fork 503
fix: close stream after upstream ends to prevent client hang #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,18 +259,26 @@ const ConversationView = () => { | |
| const reader = data.getReader(); | ||
| const decoder = new TextDecoder("utf-8"); | ||
| let done = false; | ||
| while (!done) { | ||
| const { value, done: readerDone } = await reader.read(); | ||
| if (value) { | ||
| const char = decoder.decode(value); | ||
| if (char) { | ||
| assistantMessage.content = assistantMessage.content + char; | ||
| messageStore.updateMessage(assistantMessage.id, { | ||
| content: assistantMessage.content, | ||
| }); | ||
| try { | ||
| while (!done) { | ||
| const { value, done: readerDone } = await reader.read(); | ||
| if (value) { | ||
| const char = decoder.decode(value); | ||
| if (char) { | ||
| assistantMessage.content = assistantMessage.content + char; | ||
| messageStore.updateMessage(assistantMessage.id, { | ||
| content: assistantMessage.content, | ||
| }); | ||
| } | ||
| } | ||
| done = readerDone; | ||
| } | ||
| done = readerDone; | ||
| } catch (error) { | ||
| messageStore.updateMessage(assistantMessage.id, { | ||
| content: assistantMessage.content || "Failed to receive response. Please check your API endpoint configuration.", | ||
| status: "FAILED", | ||
| }); | ||
| return; | ||
|
Comment on lines
+262
to
+281
|
||
| } | ||
| messageStore.updateMessage(assistantMessage.id, { | ||
| status: "DONE", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,8 +140,10 @@ const handler = async (req: NextRequest) => { | |
| try { | ||
| const json = JSON.parse(data); | ||
| const text = json.choices[0].delta?.content; | ||
| const queue = encoder.encode(text); | ||
| controller.enqueue(queue); | ||
| if (text) { | ||
| const queue = encoder.encode(text); | ||
| controller.enqueue(queue); | ||
| } | ||
| } catch (e) { | ||
| controller.error(e); | ||
| } | ||
|
|
@@ -151,6 +153,14 @@ const handler = async (req: NextRequest) => { | |
| for await (const chunk of remoteRes.body as any) { | ||
| parser.feed(decoder.decode(chunk)); | ||
| } | ||
| // Ensure the stream is closed after all upstream chunks are consumed. | ||
| // Some providers (e.g. Ollama native API) may not send a [DONE] sentinel, | ||
| // which would leave the ReadableStream open and the client hanging indefinitely. | ||
| try { | ||
| controller.close(); | ||
|
Comment on lines
+156
to
+160
|
||
| } catch { | ||
| // Already closed by the [DONE] handler — ignore. | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TextDecoder.decode(value)is called without{ stream: true }inside a loop reading arbitrary byte chunks. This can corrupt multibyte UTF-8 characters that span chunk boundaries. Use streaming decode (decoder.decode(value, { stream: true })) and flush after the loop to ensure correct character reconstruction.