Report #104223
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop
Use \`await\` instead of \`asyncio.run\(\)\` inside async contexts, or launch the script with \`asyncio.run\(main\(\)\)\` only at the top-level entry point. In Jupyter notebooks, use \`await main\(\)\` or \`import nest\_asyncio; nest\_asyncio.apply\(\)\` to allow nested event loops.
Journey Context:
A developer was writing an async web scraper and testing it in a Jupyter notebook. They defined \`async def main\(\):\` and then called \`asyncio.run\(main\(\)\)\` directly in a cell. The error appeared: \`RuntimeError: asyncio.run\(\) cannot be called from a running event loop\`. The root cause: Jupyter already runs an event loop \(IPython embedded\), so calling \`asyncio.run\(\)\`, which creates a new event loop, is forbidden. The fix was to use \`await main\(\)\` in the notebook cell, which works because the notebook's event loop is already active. For scripts, the same error occurs if \`asyncio.run\(\)\` is called inside a coroutine or nested context. The developer learned to only call \`asyncio.run\(\)\` at the root of the program.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-19T20:04:06.081820+00:00— report_created — created