Report #75359
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop
Use \`await\` directly on the coroutine if already in an async context, or use \`asyncio.create\_task\(\)\`/\`await\` instead of \`asyncio.run\(\)\`. \`asyncio.run\(\)\` is designed to be the main entry point for standalone async programs—it creates a new event loop, runs the coroutine, and closes the loop. When code is already executing inside an event loop \(e.g., in Jupyter/IPython, an existing async web framework, or a nested async call\), calling \`asyncio.run\(\)\` attempts to create a second loop, which is forbidden. The fix leverages the existing loop by awaiting the coroutine directly or using lower-level APIs like \`create\_task\(\)\`.
Journey Context:
You're in a Jupyter notebook trying to test an async function. You define \`async def fetch\(\): ...\` then try to test it with \`asyncio.run\(fetch\(\)\)\`. You immediately get 'RuntimeError: asyncio.run\(\) cannot be called from a running event loop'. You try \`loop = asyncio.get\_event\_loop\(\); loop.run\_until\_complete\(fetch\(\)\)\` and get 'RuntimeError: This event loop is already running'. You're confused because this works in a \`.py\` file but not in Jupyter. You search and discover that Jupyter/IPython runs its own asyncio loop in the background for interactive execution. \`asyncio.run\(\)\` is strictly for the 'main' program entry point. You realize you can just \`await fetch\(\)\` directly in the notebook cell because you're already in an async context, or use \`asyncio.create\_task\(\)\` if you need fire-and-forget. The fix works because it respects the single-threaded event loop architecture—only one loop can run per thread, and Jupyter already owns it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:05:29.564328+00:00— report_created — created