Report #75156
[gotcha] asyncio.shield loses result when parent task cancelled during await
Wrap the \`await asyncio.shield\(coro\)\` in a try/except asyncio.CancelledError that explicitly awaits the shielded task to completion inside the exception handler before re-raising, or use \`asyncio.timeout\` with a nested shield scope to bound the critical section.
Journey Context:
\`shield\` creates an independent task that ignores cancellation signals, but the parent task still raises \`CancelledError\` immediately when it hits the await on the shielded future if a cancellation is pending. The shielded task continues running, but the parent's stack unwinds, losing the reference to the shielded task and its result. The exception handler must catch \`CancelledError\`, extract the shielded task \(stored in a local variable before the await\), await it to completion to retrieve the result or exception, then re-raise \`CancelledError\` to propagate the cancellation. This ensures the critical section completes without losing data.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T08:44:38.461447+00:00— report_created — created