Report #100057
[bug\_fix] \`asyncio.run\(\)\` raises \`RuntimeError: Cannot run event loop inside another event loop\` in Jupyter, IPython, or an already-running event loop
In environments that already run an event loop \(Jupyter, IPython, some web frameworks\), use \`await\` directly on the coroutine or use \`asyncio.create\_task\(\)\` / \`loop.create\_task\(\)\` rather than \`asyncio.run\(\)\`. For libraries that only expose a sync wrapper, use \`nest\_asyncio.apply\(\)\` as a compatibility shim, or run the async code in a separate thread with its own loop via \`asyncio.run\_coroutine\_threadsafe\(\)\`.
Journey Context:
A developer writes a clean async helper and calls it from a Jupyter notebook with \`asyncio.run\(main\(\)\)\`. Instead of running, the cell raises \`RuntimeError: Cannot run the event loop while another loop is running\` or \`asyncio.run\(\) cannot be called from a running event loop\`. They try wrapping it in \`asyncio.get\_event\_loop\(\).run\_until\_complete\(main\(\)\)\`, which fails with a similar message. The root cause is that Jupyter/IPython already manages a running event loop per kernel; \`asyncio.run\(\)\` is designed to create and tear down a fresh loop as the top-level entry point, so it cannot be nested. The fix works because \`await\` schedules the coroutine on the existing loop, and \`nest\_asyncio\` patches the loop to allow nested execution where no architectural alternative exists.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-01T04:34:52.077214+00:00— report_created — created