claude-code-sdk-python/src/claude_code_sdk
Dickson Tsai fbda510ee4
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
Fix subprocess deadlock with MCP servers via stderr redirection (#103)
## Summary

Fixes a critical deadlock issue that occurs when MCP servers produce
verbose stderr output. The SDK would hang indefinitely when the stderr
pipe buffer filled up.

## The Problem

The deadlock occurred due to sequential reading of subprocess streams:
1. SDK reads stdout completely before reading stderr
2. When stderr pipe buffer fills (64KB on Linux, 16KB on macOS),
subprocess blocks on write
3. Subprocess can't continue to stdout, parent waits for stdout →
**DEADLOCK** 🔒

## The Solution  

Redirect stderr to a temporary file instead of a pipe:
- **No pipe buffer** = no possibility of deadlock
- Temp file can grow as needed (no 64KB limit)
- Still capture stderr for error reporting (last 100 lines)
- Works consistently across all async backends

## Implementation Details

- `stderr=tempfile.NamedTemporaryFile()` instead of `stderr=PIPE`
- Use `deque(maxlen=100)` to keep only recent stderr lines in memory
- Temp file is automatically cleaned up on disconnect
- Add `[stderr truncated, showing last 100 lines]` message when buffer
is full

## Testing

- Verified no deadlock with 150+ lines of stderr output
- Confirmed stderr is still captured for error reporting
- All existing tests pass
- Works with asyncio, trio, and other anyio backends

## Impact

- Fixes consistent hangs in production with MCP servers
- No functional regression - stderr handling is preserved
- Simpler than concurrent reading alternatives
- More robust than pipe-based solutions

Fixes the issue reported in Slack where SDK would hang indefinitely when
receiving messages from MCP servers with verbose logging.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-31 11:42:20 -07:00
..
_internal Fix subprocess deadlock with MCP servers via stderr redirection (#103) 2025-07-31 11:42:20 -07:00
__init__.py chore: bump version to 0.0.18 (#102) 2025-07-31 11:02:29 -07:00
_errors.py Remove hardcoded timeout for control messages to match Typescript SDK 2025-07-19 20:43:07 -07:00
client.py Make streaming implementation trio-compatible (#84) 2025-07-22 23:31:42 -07:00
py.typed Initial Python SDK import 2025-06-12 00:16:19 -07:00
query.py Fix lint and test 2025-07-19 20:04:58 -07:00
types.py Improve UserMessage types to include ToolResultBlock (#101) 2025-07-31 07:51:39 -07:00