> ## Content Index
> Fetch the complete content index at: https://debugly.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# Why Python’s lru_cache Fails on the Second Await
- URL: https://debugly.dev/python-lru-cache-async-coroutine-reuse/
- Published: 2026-11-13T17:46:00.000Z
- Updated: 2026-11-13T17:46:00.000Z
- Description: A cache hit can return an exhausted coroutine instead of a completed result. Trace why lru_cache breaks repeated async calls and choose an async-aware cache.
- Author: Rohit Bhadani
- Tags: Python, Caching, Debugging

The first call works. The second raises `RuntimeError: cannot reuse already awaited coroutine`. If an `async def` function is wrapped in `functools.lru_cache`, inspect what the cache actually holds. It caches the coroutine object returned by calling the function, not the value produced by awaiting it. Remove that decorator from the async function; use a cache designed for async results, or move synchronous memoization to a suitable synchronous helper.

*This is an AI-prepared, documentation-based explainer, not a personally reproduced incident. The walkthrough is illustrative. See the* [*editorial policy*](https://debugly.dev/editorial-policy/)*.*

## A cache hit can return the wrong kind of reusable thing

Imagine an application that loads a label by key. Someone adds `@lru_cache` because the same key is requested repeatedly. The first request succeeds. A new key works too. The repeated key fails, so it’s tempting to suspect the network client, the event loop, or the caller that added a second `await`. The cache even reports a hit. Technically, it found exactly what it saved. Customer satisfaction was apparently outside the return type.

This small illustrative example removes the network from the question:

```
import asyncio
from functools import lru_cache

@lru_cache(maxsize=4)
async def label(key):
    return key.upper()

async def main():
    first = label("a")
    print(await first)             # expected: A
    second = label("a")
    print(first is second)         # expected: True
    print(label.cache_info())      # expected: one hit, one miss
    print(await second)            # expected: RuntimeError

asyncio.run(main())
```

Calling a coroutine function creates a coroutine object. The function’s body runs when that object is driven by awaiting it or scheduling it. The ordinary cache wrapper sees that object as the call’s return value and retains it. After the first await completes, the next cache hit returns that same exhausted object. Python’s [coroutine object contract](https://docs.python.org/3/reference/datamodel.html?ref=debugly.dev#coroutine-objects) forbids awaiting a coroutine more than once.

The [functools documentation](https://docs.python.org/3/library/functools.html?ref=debugly.dev#functools.lru%5Fcache) explicitly excludes async functions from the intended use of this cache. The problem isn’t that Python forgot to cache the result. The wrapper and the caller disagree about which return boundary counts as the result.

## Follow the hit before following the traceback

Reproduce the failure with the same key twice, sequentially, in a disposable test. Compare the object returned before awaiting it, then check the cache’s hit and miss counts. A new key can appear to repair the issue because it causes a miss and creates a fresh coroutine. Clearing the cache can do the same thing temporarily. Neither changes the type of object the wrapper stores.

If the function contains I/O, replace it with a constant result for this diagnostic. If the same failure remains, a server retry or connection-pool change won’t fix the cache boundary. If no cache is involved, look for another place that stores and reuses a coroutine object, such as a field initialized once and awaited on every request. Avoid printing arguments or cached values that contain credentials or user data.

Concurrent callers can expose a related failure before the coroutine finishes: the same coroutine object may already be in use by another await. Don’t turn the sequential example into a concurrency guarantee. Test those paths separately once you’ve chosen the replacement.

## Choose what the application should share

If you only need to reuse a deterministic synchronous computation, keep `lru_cache` on that helper. The async boundary can await fresh I/O and then call the memoized helper with an appropriate, hashable input. That caches the helper’s returned value; it does not cache the remote fetch. Consider whether returned mutable objects should be shared, and whether keys need an account or tenant scope.

If you need to reuse the completed result of an async operation, choose a cache that understands that lifecycle. For example, the [async-lru project](https://github.com/aio-libs/async-lru?ref=debugly.dev) documents `alru_cache` for async functions. Evaluate its supported Python versions and documented invalidation, expiration and shutdown behavior before integrating it. A bounded entry count alone is not a freshness policy.

Write down whether two callers for the same key should share one in-flight operation, and what happens when one caller cancels, the operation raises, or its result becomes stale. Those decisions distinguish caching completed values from deduplicating work in progress. They also keep “cache the fetch” from quietly turning into “keep this tenant’s answer forever.”

## A cached Task needs an owner

Wrapping the coroutine in `asyncio.create_task()` may make a completed operation awaitable again, but that doesn’t make `lru_cache` an async cache. A Task belongs to an event loop and retains its completed outcome. A cache that stores Tasks also needs policies for cancellation, errors, eviction, loop lifetime and cleanup. Evicting an entry doesn’t, by itself, define what happens to any work still running.

The [asyncio task documentation](https://docs.python.org/3/library/asyncio-task.html?ref=debugly.dev) is the contract to check at that boundary. Debugly’s [shielded-timeout walkthrough](https://debugly.dev/python-asyncio-shield-wait-for-timeout/) covers a separate ownership question: a caller can stop waiting while work continues. Sharing work through a cache makes that question more consequential, not less.

For regression checks, cover the same key twice, two different keys, overlapping callers, a failing operation, invalidation and shutdown. Assert both the returned values and how many underlying operations ran. Then verify the freshness and tenant-scoping rules your application actually requires. A cache hit is useful only when the thing you get back is safe to reuse.