claude-code-sdk-python/tests
Ashwin Bhat 9dd6fe5f91
Fix Windows subprocess stdin buffering issue causing ClaudeSDKClient hangs (#208)
## Problem
ClaudeSDKClient initialization would hang indefinitely on Windows, timing out
after 60 seconds. The SDK successfully spawned the Claude CLI subprocess but
control requests sent via stdin never reached the subprocess due to Windows
subprocess stdin buffering behavior with Python's asyncio.

## Root Cause
On Windows, when using asyncio subprocess streams, data written to stdin can
remain buffered and not immediately sent to the child process. The CLI
subprocess waits for the initialization request that's stuck in Python's
buffer, causing the 60-second timeout.

## Solution
1. Added `flush_stdin()` method to Transport base class (non-abstract, default no-op)
2. Implemented Windows-specific flush in SubprocessCLITransport that calls
   `drain()` on the asyncio StreamWriter when available
3. Call `flush_stdin()` after all control protocol writes in Query class:
   - After sending control requests (_send_control_request)
   - After responding to incoming requests (_handle_control_request)

## Tests Added
- test_flush_stdin_on_windows: Verifies drain() called on Windows
- test_flush_stdin_on_non_windows: Verifies no-op on other platforms
- test_flush_stdin_without_process: Tests graceful handling of missing process
- test_flush_stdin_fallback_to_inner_stream: Tests wrapped stream fallback
- test_flush_stdin_called_after_control_requests: Integration test for outgoing requests
- test_flush_stdin_called_after_control_responses: Integration test for incoming requests

All tests pass on macOS, and the fix is platform-specific to Windows only.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-07 18:28:51 -07:00
..
conftest.py Initial Python SDK import 2025-06-12 00:16:19 -07:00
test_changelog.py Add changelog and changelog format check (#77) 2025-07-21 10:53:01 -07:00
test_client.py Rename claude_code to claude_agent (#188) 2025-09-28 14:52:53 -07:00
test_errors.py Rename claude_code to claude_agent (#188) 2025-09-28 14:52:53 -07:00
test_integration.py refactor: remove unnecessary node installation check (#189) 2025-09-28 15:16:40 -07:00
test_message_parser.py Rename claude_code to claude_agent (#188) 2025-09-28 14:52:53 -07:00
test_sdk_mcp_integration.py Rename claude_code to claude_agent (#188) 2025-09-28 14:52:53 -07:00
test_streaming_client.py Fix Windows subprocess stdin buffering issue causing ClaudeSDKClient hangs (#208) 2025-10-07 18:28:51 -07:00
test_subprocess_buffering.py Make CLI buffer limit configurable (#190) 2025-09-28 15:45:06 -07:00
test_tool_callbacks.py update PermissionResult to match latest control protocol (#209) 2025-10-06 14:01:16 -07:00
test_transport.py Fix Windows subprocess stdin buffering issue causing ClaudeSDKClient hangs (#208) 2025-10-07 18:28:51 -07:00
test_types.py Update model references to claude-sonnet-4-5 (#198) 2025-09-30 12:59:14 -07:00