Report #29905
[bug\_fix] RuntimeError: cannot close a running event loop or asyncio.run\(\) fails with 'event loop is running' in Jupyter/IPython.
In Jupyter/IPython, replace \`asyncio.run\(coroutine\(\)\)\` with \`await coroutine\(\)\` directly in the cell. For nested loops in scripts, use \`nest\_asyncio.apply\(\)\`. Root cause: Jupyter maintains a global event loop running in the background to handle kernel communication. \`asyncio.run\(\)\` attempts to create a new loop, run the coroutine, and close the loop, which is prohibited when a loop is already running. \`await\` schedules the coroutine on the existing loop.
Journey Context:
A developer writes an async function \`async def fetch\(\): ...\` using \`aiohttp\`. In a Jupyter notebook cell, they write \`asyncio.run\(fetch\(\)\)\` as they would in a standard script. Executing the cell raises \`RuntimeError: cannot close a running event loop\`. The developer tries \`loop = asyncio.get\_event\_loop\(\); loop.run\_until\_complete\(fetch\(\)\)\`, which either works but warns about deprecation or fails with 'loop already running'. They search StackOverflow and discover that Jupyter is async-native. They simply replace the cell content with \`await fetch\(\)\`, which executes immediately because the cell runs inside Jupyter's existing event loop.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T04:35:06.649965+00:00— report_created — created