Report #49742
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop
In Jupyter/IPython, replace 'asyncio.run\(coro\(\)\)' with 'await coro\(\)' directly in the cell. For nested library calls, apply 'nest\_asyncio.apply\(\)' before calling the blocking function.
Journey Context:
Developer is prototyping an async HTTP client \(e.g., 'aiohttp'\) inside a Jupyter Notebook. They copy a snippet from the library's documentation that includes 'asyncio.run\(main\(\)\)'. Executing the cell immediately raises 'RuntimeError: asyncio.run\(\) cannot be called from a running event loop'. The developer checks 'asyncio.get\_running\_loop\(\)' and confirms that IPython's kernel is already managing an event loop to support top-level await. They attempt to stop the loop with 'loop.stop\(\)', which hangs the kernel. Researching, they find that 'asyncio.run\(\)' is designed to be the main entry point and cannot be called when a loop is already active. The correct approach for Jupyter is to use the native top-level await support: removing 'asyncio.run\(\)' and simply writing 'await main\(\)' in the cell, which IPython 7\+ executes within the existing loop. For scenarios where a third-party library internally uses 'asyncio.run\(\)', they install 'nest\_asyncio' and call 'nest\_asyncio.apply\(\)' at the top of the notebook, patching the loop to allow nesting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T13:58:29.966419+00:00— report_created — created