fix: add stream idle timeout to prevent sessions from hanging indefinitely #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
When network issues occur mid-stream (TCP half-open connections, stalled LLM providers, proxy timeouts), the streaming loop would hang forever waiting for the next chunk. This fix adds an idle timeout that detects when no data has been received for a configurable period (default 60 seconds) and triggers a retry with exponential backoff.
Problem
The
for await (const value of stream.fullStream)loop inprocessor.tshas no idle timeout. If the network drops mid-stream or the LLM provider stops sending data without closing the connection, the loop hangs indefinitely.Related issues: anomalyco#8383, anomalyco#2512, anomalyco#2819, anomalyco#4255
Solution
StreamIdleTimeoutErrorclass to detect stalled streamswithIdleTimeout()async generator wrapper that races each chunk against a timeoutstream.fullStreamwith the idle timeout in the process loopStreamIdleTimeoutErroras retryable so sessions recover automaticallystream_idle_timeoutconfig option under experimental settings (default: 60s)Testing
withIdleTimeout()logic (normal streams, stalled streams, slow streams, abort signals)Configuration
Users can adjust the timeout in their
opencode.json:{ "experimental": { "stream_idle_timeout": 60000 } }Set to
0to disable the idle timeout entirely.