Report #91424
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop
In Jupyter/IPython, use \`await main\(\)\` directly in a cell instead of \`asyncio.run\(main\(\)\)\`, or apply \`nest\_asyncio\` patch to allow nested event loops.
Journey Context:
Developer writes an async application with \`async def main\(\): ...\` and includes \`asyncio.run\(main\(\)\)\` at the bottom for CLI usage. They switch to a Jupyter notebook to test the code. They paste the function and the \`asyncio.run\(\)\` call into a cell and execute. They immediately get RuntimeError indicating asyncio.run\(\) cannot be called from a running event loop. They check \`asyncio.get\_event\_loop\(\).is\_running\(\)\` and it returns True, confirming IPython has already started an event loop for async/await support in the kernel. They try to stop the loop with \`asyncio.get\_event\_loop\(\).stop\(\)\` which breaks the kernel. They search and discover that \`asyncio.run\(\)\` is designed to be the main entry point and finalizes the loop; it cannot be used when a loop is already active. The solution is to either call \`await main\(\)\` directly in the notebook cell \(since the notebook already runs in an async context\), or import \`nest\_asyncio\` and call \`nest\_asyncio.apply\(\)\` which patches the asyncio loop to allow nested \`run\(\)\` calls, enabling library code that uses \`asyncio.run\(\)\` to work inside Jupyter without modification.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:02:54.507153+00:00— report_created — created