Repository navigation
fix: add stream idle timeout for hung proxy connections - #31558
neonsecret wants to merge 1 commit into
Conversation
When a proxy (e.g. LiteLLM) accepts an API request and returns HTTP 200 headers but the backend (e.g. Vertex AI) stalls, the SSE stream hangs indefinitely. The OpenAI SDK's 10-minute timeout only covers the initial fetch — once headers arrive, it's cleared. This patch adds a 120-second idle timeout to the openai-completions streaming loop. The timer resets on every SSE chunk. If no chunk arrives within 120s, the stream's AbortController fires, killing the hung connection. The agent loop then handles the error (retry or report). This does not affect the overall session timeout (configurable via agents.defaults.timeoutSeconds) — active streams where chunks keep flowing are unaffected. Co-Authored-By: Claude Opus 4.6 <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 61a4f73fa2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } | ||
| } | ||
| } | ||
| + clearTimeout(_idleTimer); |
There was a problem hiding this comment.
Clear idle timer in a finally block
The new idle timeout is only cleared on the normal end-of-stream path, but for await (const chunk of openaiStream) can exit via throw (for example user-initiated aborts or stream/parsing errors). In those cases _idleTimer remains scheduled for up to 120 seconds, which retains references and can keep one-off Node processes alive unexpectedly; wrapping the loop in try { ... } finally { clearTimeout(_idleTimer) } avoids this leak across all exit paths.
Useful? React with 👍 / 👎.
Greptile SummaryThis PR addresses a real and well-described problem: proxies like LiteLLM can return HTTP 200 immediately, causing the OpenAI SDK's fetch timeout to clear before any SSE data arrives, leaving the streaming loop to hang silently. The fix wraps the Key observations:
Confidence Score: 4/5
Last reviewed commit: 61a4f73 |
| + let _idleTimer = setTimeout(() => openaiStream.controller.abort(), _IDLE_MS); | ||
| for await (const chunk of openaiStream) { | ||
| + clearTimeout(_idleTimer); | ||
| + _idleTimer = setTimeout(() => openaiStream.controller.abort(), _IDLE_MS); | ||
| if (chunk.usage) { | ||
| const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens || 0; | ||
| const reasoningTokens = chunk.usage.completion_tokens_details?.reasoning_tokens || 0; | ||
| @@ -241,6 +248,7 @@ | ||
| } | ||
| } | ||
| } | ||
| + clearTimeout(_idleTimer); |
There was a problem hiding this comment.
Missing clearTimeout on exception path
If an exception is thrown during chunk processing (inside the for await loop body, after _idleTimer is reset but before the loop iterates again), the clearTimeout(_idleTimer) at the bottom is never reached. This leaves a dangling 120s timer that will eventually call openaiStream.controller.abort() on an already-errored or garbage-collectable stream.
A try/finally block would guarantee cleanup on all exit paths — normal completion, early return, and thrown exceptions:
const _IDLE_MS = 120000;
let _idleTimer = setTimeout(() => openaiStream.controller.abort(), _IDLE_MS);
try {
for await (const chunk of openaiStream) {
clearTimeout(_idleTimer);
_idleTimer = setTimeout(() => openaiStream.controller.abort(), _IDLE_MS);
// ... rest of chunk processing
}
} finally {
clearTimeout(_idleTimer);
}
finishCurrentBlock(currentBlock);This also handles the case where openaiStream.controller.abort() fires (the intended timeout path): the finally block runs after the AbortError propagates out of the loop, and clearTimeout on an already-fired timer is harmless.
Prompt To Fix With AI
This is a comment left during a code review.
Path: patches/@[email protected]
Line: 13-24
Comment:
**Missing `clearTimeout` on exception path**
If an exception is thrown during chunk processing (inside the `for await` loop body, after `_idleTimer` is reset but before the loop iterates again), the `clearTimeout(_idleTimer)` at the bottom is never reached. This leaves a dangling 120s timer that will eventually call `openaiStream.controller.abort()` on an already-errored or garbage-collectable stream.
A `try/finally` block would guarantee cleanup on all exit paths — normal completion, early return, and thrown exceptions:
```javascript
const _IDLE_MS = 120000;
let _idleTimer = setTimeout(() => openaiStream.controller.abort(), _IDLE_MS);
try {
for await (const chunk of openaiStream) {
clearTimeout(_idleTimer);
_idleTimer = setTimeout(() => openaiStream.controller.abort(), _IDLE_MS);
// ... rest of chunk processing
}
} finally {
clearTimeout(_idleTimer);
}
finishCurrentBlock(currentBlock);
```
This also handles the case where `openaiStream.controller.abort()` fires (the intended timeout path): the `finally` block runs after the `AbortError` propagates out of the loop, and `clearTimeout` on an already-fired timer is harmless.
How can I resolve this? If you propose a fix, please make it concise.|
Closing as duplicate; this was superseded by #55072. |
Summary
openai-completionsstreaming loop via a pnpm patch on@mariozechner/pi-aiProblem
When using OpenClaw with a proxy like LiteLLM in front of Vertex AI:
runTimeoutSeconds(default 600s, configurable up to hours) can kill itThis caused subagent sessions to hang for hours with no output.
Fix
The patch wraps the
for await (const chunk of openaiStream)loop with an idle timer:openaiStream.controller.abort()Test plan
🤖 Generated with Claude Code