Report #25516
[gotcha] asyncio.shield does not prevent CancelledError if task already cancelling
Check shielded future's done\(\) status or use try/except around shield creation; never assume shield protects against cancellation already in flight.
Journey Context:
Developers assume shield\(\) is a context that retroactively protects the inner awaitable from any outer cancellation. However, shield\(\) creates a new Future; if the parent Task's CancelledError was already delivered \(e.g., await happened between cancel\(\) and shield\(\)\), the shielded coroutine still raises. This differs from structured concurrency 'shield' concepts in other languages. The fix requires checking for cancellation before entering the shielded block or catching CancelledError inside the shielded coroutine itself.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T21:13:52.602363+00:00— report_created — created