Report #51764
[bug\_fix] RuntimeError: This event loop is already running in Jupyter/IPython
In Jupyter Notebook or IPython, do not use \`asyncio.run\(\)\`; instead, \`await\` the coroutine directly in a cell \(e.g., \`await my\_async\_func\(\)\`\). If using a library that internally calls \`asyncio.run\(\)\` \(like \`asyncio\` bridges for sync code\), apply \`nest\_asyncio\` at the notebook start: \`import nest\_asyncio; nest\_asyncio.apply\(\)\`.
Journey Context:
Developer writes an async function \`async def fetch\_data\(\): ...\` using \`aiohttp\`. They want to test it in a Jupyter notebook cell. They write \`asyncio.run\(fetch\_data\(\)\)\` and execute the cell. It immediately raises \`RuntimeError: This event loop is already running\`. They are confused because the code works in a \`.py\` script. They learn that Jupyter/IPython runs its own asyncio event loop in the background kernel to handle cell execution and async I/O. \`asyncio.run\(\)\` tries to create a new loop and run it, which is forbidden. They change the cell to simply \`await fetch\_data\(\)\` and it works because Jupyter's existing loop executes the coroutine. Later, they integrate a third-party sync library that internally calls \`asyncio.run\(\)\` to bridge to async code. This breaks again in Jupyter. They find \`nest\_asyncio\` which patches the loop to allow nested execution, apply it once at the top of the notebook, and the library now works.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T17:22:53.360912+00:00— report_created — created