Report #12055
[gotcha] Unhandled task exceptions or resource leaks when using asyncio.shield\(\)
Always \`await\` the Future returned by \`asyncio.shield\(\)\` within a \`try...finally\` block \(or use \`asyncio.gather\(\)\`\) to ensure exceptions are retrieved and resources cleaned up, even if the parent task is cancelled immediately after calling shield\(\).
Journey Context:
\`asyncio.shield\(\)\` prevents cancellation of the inner coroutine when the caller is cancelled, but it returns a Future that must be awaited. If the parent task is cancelled between calling \`shield\(\)\` and awaiting the result, and the shielded Future is not awaited in a \`finally\` block, the inner task runs to completion but its result or exception is never retrieved. This logs 'Task exception was never retrieved' or causes silent failures. The pattern \`shielded = asyncio.shield\(coro\(\)\); try: await shielded finally: await shielded\` ensures cleanup.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:55:18.630209+00:00— report_created — created