Skip to content

fix: handle transport close race condition in control request responses#492

Open
drillan wants to merge 1 commit intoanthropics:mainfrom
drillan:fix/string-prompt-mcp-transport
Open

fix: handle transport close race condition in control request responses#492
drillan wants to merge 1 commit intoanthropics:mainfrom
drillan:fix/string-prompt-mcp-transport

Conversation

@drillan
Copy link

@drillan drillan commented Jan 19, 2026

Summary

  • When the CLI process exits immediately after processing the final message, the transport may close before we can send the control response
  • This caused unhandled CLIConnectionError exceptions during _handle_control_request
  • Wrap transport.write calls with try/except CLIConnectionError to gracefully handle the race condition

Changes

  • Added CLIConnectionError import to query.py
  • Wrapped success and error response transport.write calls in _handle_control_request with try/except to handle transport close race condition
  • Other exceptions still propagate normally

Test plan

  • Added test_query_control_request.py with 6 test cases:
    • Success response on closed transport
    • Error response on closed transport
    • MCP message on closed transport
    • Normal operation unaffected
    • Hook callback on closed transport
    • Other exceptions still propagate
  • All 162 tests pass

Related to #386

🤖 Generated with Claude Code

drillan added a commit to drillan/claudecode-model that referenced this pull request Jan 19, 2026
Use forked branch to resolve ProcessTransport error when using SDK MCP
servers with string prompts.

- Add [tool.uv.sources] to use forked branch
- Bump version from 0.0.3 to 0.0.4

Upstream PR: anthropics/claude-agent-sdk-python#492

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
When the CLI process exits immediately after processing the final
message, the transport may close before we can send the control
response. This caused unhandled CLIConnectionError exceptions.

Wrap transport.write calls in _handle_control_request with try/except
to gracefully handle the race condition when the transport is already
closed. Other exceptions still propagate normally.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@drillan drillan force-pushed the fix/string-prompt-mcp-transport branch from c1b9f8d to f57ed08 Compare February 15, 2026 01:53
@drillan drillan changed the title fix: force streaming mode for string prompts with SDK MCP servers fix: handle transport close race condition in control request responses Feb 15, 2026
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.

1 participant