Report #53948
[bug\_fix] RuntimeError: Event loop is closed when using asyncio in Jupyter or tests
In Jupyter notebooks/IPython, use 'await' directly without 'asyncio.run\(\)' since the kernel already has a running loop; in scripts, ensure 'asyncio.run\(\)' is called only once at the entry point and never inside nested async functions. Root cause: 'asyncio.run\(\)' closes the event loop after the coroutine completes; in Jupyter, the kernel maintains a persistent loop that cannot be restarted once closed, and nested calls to run\(\) try to close an already closed loop or conflict with the running loop.
Journey Context:
Developer writes an async function 'async def fetch\(\): ...' in a Jupyter notebook. In the first cell they run 'asyncio.run\(fetch\(\)\)' and it works. In the second cell they try to run it again and get 'RuntimeError: Event loop is closed'. They try to restart the kernel and it works once then fails again. They search and find that 'asyncio.run\(\)' is meant to be called once per program. They realize Jupyter already has an event loop running \(via tornado\). They remove 'asyncio.run\(\)' and just type 'await fetch\(\)' in the cell, and it works repeatedly. The root cause was that run\(\) closes the loop, and Jupyter can't reopen the kernel's main loop.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T21:02:55.781352+00:00— report_created — created