Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stream-idle-timeout.md
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.
17 changes: 17 additions & 0 deletions packages/kosong/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Abort the iterator rather than the stream wrapper

This timeout calls return on the StreamedMessage wrapper, but the current provider stream classes expose only [Symbol.asyncIterator]() and keep the actual async generator private (for example KimiStreamedMessage at packages/kosong/src/providers/kimi.ts:218-255, and the OpenAI/Anthropic/Google variants follow the same pattern). In those normal streaming paths stream.return is undefined, so when no chunk arrives the timer fires but does not unblock the pending for await next() call, leaving the stalled connection hung.

Useful? React with 👍 / 👎.

}, IDLE_TIMEOUT_MS);
};
resetIdleTimer();

try {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Close the try block with catch/finally

This new try is never closed with a catch or finally, so generate.ts is no longer parseable; tsc -p packages/kosong/tsconfig.json --noEmit --pretty false reports TS1472: 'catch' or 'finally' expected and the package build is blocked. Add the intended finally that clears idleTimer or remove the try.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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).
Expand Down Expand Up @@ -155,6 +169,9 @@ export async function generate(
pendingPart = part;
}
}
} finally {
if (idleTimer) clearTimeout(idleTimer);
}

await throwIfAborted(options?.signal, stream);
options?.onStreamEnd?.();
Expand Down