Commit 2c8c7fd
authored
## Summary
This PR addresses the `anyio.BrokenResourceError` issue from #139 and
aligns the Python SDK's error handling with the TypeScript SDK
implementation.
## Changes
### Fix stream closure issue in Query
- Removed the `async with` context manager from
`Query.receive_messages()` that was closing the stream after first use
- The stream now remains open for the entire session, allowing multiple
queries in streaming mode
- This fixes the `BrokenResourceError` that occurred during multi-turn
conversations
### Align subprocess transport with TypeScript SDK
Following the TypeScript `ProcessTransport` implementation pattern:
- **Added `_exit_error` tracking**: Captures and preserves process-level
errors for better error propagation
- **Enhanced `write()` method checks**:
- Validates transport readiness before writing
- Checks if process is still alive (exit code)
- Checks for stored exit errors before attempting writes
- Marks transport as not ready on write failures
- **Improved error handling in `connect()`**: Stores errors as
`_exit_error` for later reference
- **Simplified `is_ready()` method**: Now just returns the `_ready`
flag, matching TypeScript's simpler approach
### Other improvements
- Added asyncio pytest plugin configuration (`-p asyncio` in
pyproject.toml)
- Added clarifying comment about TextReceiveStream line handling
## Testing
The multi-turn conversation example now works correctly:
```bash
python examples/streaming_mode.py multi_turn_conversation
```
## Related Issues
Fixes #139
1 parent 681f46c commit 2c8c7fd
File tree
3 files changed
+50
-27
lines changed- src/claude_code_sdk/_internal
- transport
3 files changed
+50
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
456 | 456 | | |
457 | 457 | | |
458 | 458 | | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
468 | 467 | | |
469 | 468 | | |
470 | 469 | | |
| |||
Lines changed: 41 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
216 | | - | |
| 217 | + | |
217 | 218 | | |
218 | | - | |
219 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
220 | 225 | | |
221 | | - | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
222 | 229 | | |
223 | 230 | | |
224 | 231 | | |
| |||
259 | 266 | | |
260 | 267 | | |
261 | 268 | | |
| 269 | + | |
262 | 270 | | |
263 | 271 | | |
264 | 272 | | |
265 | | - | |
266 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
267 | 288 | | |
268 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
269 | 297 | | |
270 | 298 | | |
271 | 299 | | |
272 | 300 | | |
273 | 301 | | |
274 | 302 | | |
275 | 303 | | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | 304 | | |
280 | 305 | | |
281 | 306 | | |
| |||
295 | 320 | | |
296 | 321 | | |
297 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
298 | 326 | | |
299 | 327 | | |
300 | 328 | | |
| |||
361 | 389 | | |
362 | 390 | | |
363 | 391 | | |
364 | | - | |
| 392 | + | |
365 | 393 | | |
366 | 394 | | |
367 | 395 | | |
368 | 396 | | |
| 397 | + | |
369 | 398 | | |
370 | 399 | | |
371 | 400 | | |
372 | 401 | | |
373 | 402 | | |
374 | 403 | | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
| 404 | + | |
0 commit comments