RuntimeWarning: coroutine 'main' was never awaited (Python) — how to fix
app.py:10: RuntimeWarning: coroutine 'main' was never awaited
main()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Quick fix: Add await in front of the call inside async code (result = await fetch()). From synchronous top-level code use asyncio.run(main()). To run it in the background use task = asyncio.create_task(fetch()) and keep a reference to task.
What it means
Calling an async def function does not run it. It returns a coroutine object. The body only executes when that object is awaited, passed to asyncio.run(), or wrapped in a task. If the coroutine is garbage-collected without any of that, Python emits this warning. Your code inside the function never ran.
async def save(item):
...
save(item) # RuntimeWarning: coroutine 'save' was never awaited
The second line, Enable tracemalloc to get the object allocation traceback, is just a hint: run with python -X tracemalloc=5 app.py to see where the coroutine was created.
Fix 1: add await
Inside another async def, await the call:
async def handler(item):
await save(item)
Common places it gets forgotten: await response.json() (aiohttp), await asyncio.sleep(1), await session.commit() (async SQLAlchemy), await page.goto(url) (Playwright async API).
Fix 2: starting from synchronous code
At the top level of a script you can't use await. Start the event loop with asyncio.run():
import asyncio
async def main():
await save(item)
if __name__ == "__main__":
asyncio.run(main()) # not just main()
In Jupyter/IPython a loop is already running, so use await main() directly in a cell.
Fix 3: fire-and-forget needs a task
If you want something to run concurrently without waiting for it right away, schedule it:
task = asyncio.create_task(save(item))
# ... do other work ...
await task
Keep a reference to the task (in a variable or a set). The event loop only holds weak references, so an unreferenced task can be garbage-collected before it finishes.
To run many at once:
results = await asyncio.gather(*(save(i) for i in items))
Fix 4: async function passed as a sync callback
Libraries that expect a normal function will call your coroutine function and throw the result away: threading.Thread(target=main), map(fetch, urls), sorted(key=...), signal handlers, or a sync web framework route.
threading.Thread(target=lambda: asyncio.run(main())).start()
Or make the callback a normal function, or use the library's async variant.
Fix 5: mocks in tests
unittest.mock.MagicMock is not awaitable. When patching an async function use AsyncMock (Python 3.8+):
from unittest.mock import AsyncMock, patch
with patch("app.save", new_callable=AsyncMock) as m:
await handler(item)
m.assert_awaited_once()
With pytest, mark async tests (for example @pytest.mark.asyncio with pytest-asyncio) or the test function itself becomes a never-awaited coroutine.
Make it an error while debugging
python -W error::RuntimeWarning -X tracemalloc=5 app.py
This turns the warning into an exception with a traceback that points to the missing await.
Last updated 2026-11-29.