Skip to content

no-async-function-cache

Standard library cache decorates a coroutine function and retains single-use coroutine objects.

Why

functools.cache, lru_cache, and cached_property store the coroutine object returned by an async function rather than its awaited value. A subsequent cache hit can raise RuntimeError: cannot reuse already awaited coroutine.

Fix

Remove the standard library cache or use an async-aware cache that stores completed results.

Examples

Before — flagged A standard cache retains an exhausted coroutine
app/cache.py
from functools import cache
@cache
async def load() -> int:
return 1
After — preferred Each call creates a fresh coroutine
app/cache.py
async def load() -> int:
return 1