Skip to content

fix: keep stdin open for hooks during multi-turn agent execution#580

Open
pjcdawkins wants to merge 1 commit intoanthropics:mainfrom
pjcdawkins:fix/keep-stdin-open-for-hooks
Open

fix: keep stdin open for hooks during multi-turn agent execution#580
pjcdawkins wants to merge 1 commit intoanthropics:mainfrom
pjcdawkins:fix/keep-stdin-open-for-hooks

Conversation

@pjcdawkins
Copy link

Summary

stream_input() closes stdin after receiving the first result message. In multi-turn agents (max_turns > 1), this breaks hook callbacks on all subsequent turns because the bidirectional control channel is gone. The CLI's sendRequest() throws "Stream closed" on every tool call after the first turn.

This PR changes stream_input() to block indefinitely (via anyio.sleep_forever()) when hooks or SDK MCP servers are configured, keeping stdin open for the entire session. Cleanup is handled by the existing close() path: the task group cancellation breaks the sleep, and transport.close() closes stdin.

  • Without hooks/MCP servers: behavior is unchanged (end_input() called immediately)
  • With hooks/MCP servers: stdin stays open until session end

Also removes the now-unused _first_result_event and _stream_close_timeout members.

Fixes #554

Test plan

  • New test: stream_input calls end_input() without hooks
  • New test: stream_input blocks (does not call end_input()) with hooks
  • Existing tests pass (158 passed)

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hook callback errors (Stream closed) on every tool call since v0.1.29

1 participant