PythonMastery
reference 3 min read · lesson 80 of 80 in Errors

RuntimeWarning: coroutine 'X' was never awaited

1 · The lesson

read

What this error means

You called a function defined with async def, but you didn't await it. Calling an async function does not run its code. It only creates a coroutine: an object that holds the work, waiting to be started. The work starts when you await it, or hand it to asyncio.run() or asyncio.create_task(). Yours was thrown away unused, and Python warns about it when it cleans the object up. So the function body never ran.

When you see it

python
main.py:8: RuntimeWarning: coroutine 'save' was never awaited
  save("book")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
done

Sometimes the warning has no useful line number, because Python only noticed while shutting down:

python
<coroutine object fetch_price at 0x0000027D38D02740>
<sys>:0: RuntimeWarning: coroutine 'fetch_price' was never awaited

The name in quotes is the async function you forgot to await.

Why it happens

1. A missing await inside async code. This is the most common case. The line looks like a normal function call, so it is easy to miss.

python
import asyncio

async def save(item):
    await asyncio.sleep(0.1)
    print("saved", item)

async def main():
    save("book")              # RuntimeWarning: never awaited
    print("done")

asyncio.run(main())           # prints only: done

2. Calling an async function from normal code. Outside async def you can't use await, so the call just makes a coroutine and nothing runs:

python
import asyncio

async def fetch_price(item):
    await asyncio.sleep(0.1)
    return 9.99

price = fetch_price("book")
print(type(price).__name__)   # coroutine: not 9.99
price.close()                 # tidy up, so this example shows no warning

3. Using the result as if it were the value. price * 2 on a coroutine fails with TypeError: unsupported operand type(s) for *: 'coroutine' and 'int', and the warning follows straight after.

How to fix it

Inside an async def, put await in front of the call:

python
import asyncio

async def save(item):
    await asyncio.sleep(0.1)
    print("saved", item)

async def main():
    await save("book")
    print("done")

asyncio.run(main())
# saved book
# done

From normal code, start it with asyncio.run(). Use it once, at the top of the program, for the main async function:

python
import asyncio

async def fetch_price(item):
    await asyncio.sleep(0.1)
    return 9.99

price = asyncio.run(fetch_price("book"))
print(price)                  # 9.99

To run several at the same time, use asyncio.gather() and await that:

python
import asyncio

async def fetch_price(item):
    await asyncio.sleep(0.1)
    return {"book": 9.99, "pen": 1.5}[item]

async def main():
    prices = await asyncio.gather(fetch_price("book"), fetch_price("pen"))
    print(prices)             # [9.99, 1.5]

asyncio.run(main())

To find where it happened, run Python with python -X tracemalloc main.py. The warning then shows the line where the coroutine was created. python -X dev main.py goes further: it also shows the chain of calls that created the coroutine, and turns on other checks for async code.

When you'd see this in real code

  • A library moved to async (newer versions of database, HTTP or chat-bot libraries), and old code still calls its functions without await.
  • In a Discord or Telegram bot: channel.send("hi") instead of await channel.send("hi"). The bot runs, and the message never appears.
  • An async function passed as a callback to code that calls it normally, like button.on_click(handler) in a framework that doesn't expect async functions.
  • asyncio.sleep(1) without await inside an async loop. Nothing pauses, and the loop runs at full speed. Python shows the warning only once, even though it happened on every pass.

See Also

Found this useful? Share it with a friend who’s learning Python.