fix: surface prompt stream errors instead of hanging query() forever#1109
Open
Echolonius wants to merge 1 commit into
Open
fix: surface prompt stream errors instead of hanging query() forever#1109Echolonius wants to merge 1 commit into
Echolonius wants to merge 1 commit into
Conversation
stream_input() caught every exception from the caller's prompt AsyncIterable (or a failed stdin write) and logged it at debug level. Stdin stayed open, the CLI kept waiting for input that would never come, no result ever arrived, and query() blocked forever with no surfaced error. On failure (outside teardown): log at error level, close stdin so the CLI can wind down, and push an error message into the message stream so receive_messages() raises instead of blocking indefinitely.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1108.
Problem
stream_input()caught every exception from the caller's promptAsyncIterable(or a failed stdin write /json.dumps) and logged it at debug level. Stdin stayed open, the CLI kept waiting for input that would never come, no result ever arrived, andquery()blocked forever with no surfaced error. Verified against the real CLI: a generator that raises hangsquery()indefinitely whether it raises before the first message or between messages.Fix
In the exception handler, when not already in teardown (
self._closed):end_input(), best-effort) so the CLI can wind down instead of waiting on input,{"type": "error"}message into the message stream — the same mechanism_read_messagesalready uses for fatal read errors — soreceive_messages()raises promptly.During teardown the old debug-and-return behavior is preserved (writes interrupted by
close()are expected noise, not actionable errors). If the read task already closed the stream, its error wins (ClosedResourceErrorsuppressed).Verification
Error streaming input: boom-...in 3-7s.test_streaming_prompt_input_stream_error_surfaces: mock transport whose stdout produces nothing on its own (like the real CLI mid-wait); prompt generator yields one message then raises. Fails on main (bounded 5s timeout — the hang); passes with the fix, and assertsend_input()was called.pytest tests/test_query.py tests/test_tool_callbacks.py tests/test_streaming_client.py: 137 passed.ruff checkandruff format --checkclean.Note: independent of #1106 (different hunks in the same file; no overlap).
🤖 Generated with Claude Code
https://claude.ai/code/session_01YcMi8ny9DUBDf6y6m76HWk