fix(cache): reject async key builders instead of failing every request - #341
Merged
Merged
Conversation
@cache(key_builder=...) accepted an async def builder and then failed every request with a TypeError and a "coroutine was never awaited" warning. The decorator now raises CacheXError when applied if the builder is a coroutine function, an object with an async __call__, or a functools.partial of either. invalidate() raises the same error before touching the backend. A builder that still returns a non-str (e.g. a sync wrapper returning a coroutine) raises CacheXError at request time; a returned coroutine is closed first. fail_open does not apply: this is a programming error, not a backend failure. Closes #323
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@cache(key_builder=...)accepted anasync defkey builder and then failed every request with a 500 (TypeError: sequence item 0: expected str instance, coroutine foundplus "coroutine was never awaited").invalidate(request, key_builder=...)had the same problem.Approach: reject async builders rather than await them. The
CacheKeyBuildertype is sync, the builder runs in more than one place, and anything async a builder needs can be resolved in a dependency or middleware and read fromrequest.state.@cacheraisesCacheXErrorif the builder is a coroutine function, an object with anasync def __call__, or afunctools.partial(nested partials too) of either. It reuses the existing_is_coroutine_callablecheck after unwrapping partials.invalidate(): runs the same check and raises before touching the backend.str, the request raisesCacheXError. A returned coroutine is closed first, so no "never awaited"RuntimeWarningappears.invalidate()does the same.fail_opendoes not apply. It is a programming error in the route, not a backend failure. The builder call already sat outside thefail_openhandling, and this is now documented.Docs: the
key_builder/invalidate()docstrings and "Raises" sections, the key builder section ofdocs/HTTP_CACHING.mdand its zh-TW mirror. Changelog fragmentchangelog.d/323.fixed.md.Tests
New tests in
tests/test_cache.py:async def, an async callable object, a partial of an async function, and a nested partial of an async callable objectinvalidate()with an async builder raises without touching the backendintraisesCacheXError, both through@cache(withfail_open=True) and throughinvalidate()All 9 new test cases fail against
master'scache.pyand pass with this change. The full suite passes with coverage at 93.9%. ruff,mypy --strictand pre-commit are clean.Closes #323