anthropics / anthropics/claude-agent-sdk-python

query() stream hangs forever (never raises) when the CLI process dies mid-run

オープン
#1,110 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug
主要言語
Python
スター
8.1k
フォーク
1.3k
PR マージ指標
PR 指標を取得中

説明

**SDK version:** 0.2.115 · **CLI:** 2.1.206 · **Platform:** macOS (darwin)

## Bug

When the `claude` subprocess dies mid-run (observed repeatedly when a Pro/Max
subscription hits its rate-limit window and the CLI exits with code 1), the
SDK logs `Fatal error in message reader: Command failed with exit code 1`
**but the `query()` async stream neither terminates nor raises** — a consumer
awaiting the next message waits forever.

## Repro sketch

```python
import asyncio
from claude_agent_sdk import ClaudeAgentOptions, query

async def main():
stream = query(
prompt="Write a long essay.",
options=ClaudeAgentOptions(model="claude-haiku-4-5", tools=[], max_turns=1),
)
async for msg in stream: # kill the spawned `claude` process mid-generation:
print(type(msg).__name__) # pkill -f "claude.*stream-json"
print("stream ended") # <- never reached; no exception either

asyncio.run(main())
```

Killing the subprocess externally reproduces it deterministically; the
organic trigger we hit was subscription rate-limit exhaustion.

## Expected

Process death should end the stream: either raise `ProcessError` (ideal, with
the exit code) or terminate the iteration, so consumers can react instead of
hanging. `receive_messages()`'s internal reader appears to catch the fatal
error without closing the memory channel the consumer is blocked on.

## Impact / workaround

Any consumer without an explicit timeout hangs indefinitely; timeouts that
cancel the iteration can additionally leak the transport if cleanup tasks are
scheduled on a loop that closes right after. We work around it in
langchain-claude-cli with a per-message inactivity watchdog plus an explicit
`stream.aclose()` executed inside the still-running loop.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。