Report #7278
[gotcha] asyncio.shield does not prevent cancellation of the inner task
Do not rely on shield to guarantee completion; instead, structure code so that cancellation is handled explicitly in the inner task, or use \`try/except asyncio.CancelledError\` inside the shielded coroutine to perform cleanup before re-raising.
Journey Context:
Developers assume \`shield\(\)\` creates an uncancellable bubble, but it only delays cancellation until the shielded awaitable finishes. If the outer task is cancelled, the shield prevents the cancellation from propagating \*during\* the await, but once the await completes, the cancellation is raised. This means cleanup code after the \`await shielded\(\)\` may never run if the shielded task itself catches CancelledError and swallows it. The alternative of using \`asyncio.create\_task\` without shielding is worse because the task dies immediately on cancellation. The correct pattern is to treat shield as a 'grace period' mechanism, not a guarantee, and always handle CancelledError explicitly in long-running cleanup code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:16:23.048530+00:00— report_created — created