Skip to content

fix(transcript_mirror): handle CancelledError in eager-flush done callback#931

Open
seeincodes wants to merge 1 commit intoanthropics:mainfrom
seeincodes:fix/transcript-mirror-batcher-cancellederror
Open

fix(transcript_mirror): handle CancelledError in eager-flush done callback#931
seeincodes wants to merge 1 commit intoanthropics:mainfrom
seeincodes:fix/transcript-mirror-batcher-cancellederror

Conversation

@seeincodes
Copy link
Copy Markdown

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

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.

…lback

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 anthropics#928's failing-test traceback).

Replace the lambda with a module-level ``_swallow_done_exception``
helper that no-ops on cancelled tasks and otherwise retrieves the
exception so asyncio doesn't warn about an unretrieved exception on a
fire-and-forget task.

Closes anthropics#930

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: TranscriptMirrorBatcher.enqueue's add_done_callback lambda raises CancelledError when task is cancelled

1 participant