fix: keep stdin open for hooks during multi-turn agent execution#580
Open
pjcdawkins wants to merge 1 commit intoanthropics:mainfrom
Open
fix: keep stdin open for hooks during multi-turn agent execution#580pjcdawkins wants to merge 1 commit intoanthropics:mainfrom
pjcdawkins wants to merge 1 commit intoanthropics:mainfrom
Conversation
stream_input() previously closed stdin after receiving the first result message, which broke hook callbacks (permission checks, security filtering, cancellation) on all subsequent agent turns. The CLI's sendRequest() threw "Stream closed" errors on every tool call. Now, when hooks or SDK MCP servers are configured, stream_input() blocks indefinitely instead of closing stdin. The task group cancellation in close() handles cleanup, and transport.close() closes stdin. Also removes the unused _first_result_event and _stream_close_timeout members that supported the old behavior. Fixes anthropics#554 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
stream_input()closes stdin after receiving the firstresultmessage. In multi-turn agents (max_turns > 1), this breaks hook callbacks on all subsequent turns because the bidirectional control channel is gone. The CLI'ssendRequest()throws"Stream closed"on every tool call after the first turn.This PR changes
stream_input()to block indefinitely (viaanyio.sleep_forever()) when hooks or SDK MCP servers are configured, keeping stdin open for the entire session. Cleanup is handled by the existingclose()path: the task group cancellation breaks the sleep, andtransport.close()closes stdin.end_input()called immediately)Also removes the now-unused
_first_result_eventand_stream_close_timeoutmembers.Fixes #554
Test plan
stream_inputcallsend_input()without hooksstream_inputblocks (does not callend_input()) with hooks🤖 Generated with Claude Code