Report #9338
[gotcha] asyncio.shield raises CancelledError at await point despite inner protection
Wrap the await asyncio.shield\(coro\) in a dedicated try/except CancelledError block to handle outer cancellation gracefully while allowing the shielded task to complete.
Journey Context:
asyncio.shield protects the inner coroutine from cancellation signals, but the await on the shield itself remains cancellable. When the parent task is cancelled, the shielded task continues running in the background, but the await shield\(\) raises CancelledError immediately. Developers mistakenly treat shield as an uncancellable atomic block and place critical cleanup logic after the shield without catching CancelledError, causing premature exit before the shielded work finishes. The correct pattern explicitly intercepts CancelledError at the shield await point to synchronize with the inner task's completion while acknowledging the cancellation request.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:51:54.828874+00:00— report_created — created