-
Notifications
You must be signed in to change notification settings - Fork 909
fix(kosong): add idle timeout to streaming response loop #1052
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Add 60s idle timeout to streaming response loop to prevent indefinite hangs. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,7 +111,21 @@ export async function generate( | |
| // the stream. | ||
| await throwIfAborted(options?.signal, stream); | ||
|
|
||
| // Idle timeout: if no chunk arrives within this period, abort the stream | ||
| // to prevent indefinite hangs when the connection stalls without closing. | ||
| const IDLE_TIMEOUT_MS = 60_000; | ||
| let idleTimer: ReturnType<typeof setTimeout> | undefined; | ||
| const resetIdleTimer = () => { | ||
| if (idleTimer) clearTimeout(idleTimer); | ||
| idleTimer = setTimeout(() => { | ||
| stream.return?.(undefined); | ||
| }, IDLE_TIMEOUT_MS); | ||
| }; | ||
| resetIdleTimer(); | ||
|
|
||
| try { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This new Useful? React with 👍 / 👎.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed — added finally block to close the try and clean up the idle timer. Also added a changeset. |
||
| for await (const part of stream) { | ||
| resetIdleTimer(); | ||
| await throwIfAborted(options?.signal, stream); | ||
|
|
||
| // Notify raw part callback (deep copy to avoid aliasing mutations). | ||
|
|
@@ -155,6 +169,9 @@ export async function generate( | |
| pendingPart = part; | ||
| } | ||
| } | ||
| } finally { | ||
| if (idleTimer) clearTimeout(idleTimer); | ||
| } | ||
|
|
||
| await throwIfAborted(options?.signal, stream); | ||
| options?.onStreamEnd?.(); | ||
|
|
||
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.
This timeout calls
returnon theStreamedMessagewrapper, but the current provider stream classes expose only[Symbol.asyncIterator]()and keep the actual async generator private (for exampleKimiStreamedMessageatpackages/kosong/src/providers/kimi.ts:218-255, and the OpenAI/Anthropic/Google variants follow the same pattern). In those normal streaming pathsstream.returnis undefined, so when no chunk arrives the timer fires but does not unblock the pendingfor awaitnext()call, leaving the stalled connection hung.Useful? React with 👍 / 👎.