Commit 9d2c650
fix(transcript_mirror): handle CancelledError in eager-flush done callback (#931)
## Summary
Fixes #930.
The `add_done_callback` lambda on the eager-flush `_flush_task` called
`t.exception()` unconditionally. In Python 3.8+, `Task.exception()`
raises `CancelledError` for cancelled tasks, and the raise from inside a
done-callback surfaces as a noisy `Exception in callback` log every time
the SDK shuts down with pending eager flushes — visible in #928's
failing-test `Captured log teardown` block.
## Changes
- `src/claude_agent_sdk/_internal/transcript_mirror_batcher.py`: replace
`lambda t: t.exception()` with a module-level `_swallow_done_exception`
helper that no-ops on cancelled tasks and otherwise retrieves the
exception so asyncio doesn't warn.
- `tests/test_transcript_mirror.py`: add `TestSwallowDoneException` with
three cases — cancelled task (must not raise), failed task (retrieves
exception, doesn't re-raise), successful task (no-op).
## Test plan
- [x] `uv run pytest
tests/test_transcript_mirror.py::TestSwallowDoneException` — 3 passed
- [x] `uv run mypy src/` — clean
- [x] `ruff check / ruff format` — clean
- [x] Pre-existing #928 failures still present (unaffected by this
change — different bug)
## Notes
The fix is intentionally narrow — same call site, same intent (silence
"Task exception was never retrieved" warnings), just cancellation-safe.
No behavior change on the happy path.
Co-authored-by: Xian Zheng <xian.zheng@challenger.gauntletai.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>1 parent 687e22f commit 9d2c650
2 files changed
Lines changed: 59 additions & 1 deletion
Lines changed: 12 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
36 | 47 | | |
37 | 48 | | |
38 | 49 | | |
| |||
88 | 99 | | |
89 | 100 | | |
90 | 101 | | |
91 | | - | |
| 102 | + | |
92 | 103 | | |
93 | 104 | | |
94 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
523 | 524 | | |
524 | 525 | | |
525 | 526 | | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
526 | 573 | | |
527 | 574 | | |
528 | 575 | | |
| |||
0 commit comments