fix: RuntimeError fallback and stampede protection - #31
Open
emiliano-go wants to merge 1 commit into
Open
emiliano-go wants to merge 1 commit into
emiliano-go wants to merge 1 commit into
Conversation
tishun
self-requested a review
August 19, 2026 12:41
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.
Changes
Bug: _store_cache_entry crashes with unhandled RuntimeError (500)
When
CachePending.redisisNone, the middleware falls back to_get_pool_state(app).get_async_client(). If no lifespan is registeredthis raises
RuntimeError, which was not caught by the existingexcept (RedisError, OSError)handler, crashing the response with a 500.Fix: Added
RuntimeErrorto the caught exception types. The error islogged as a warning and the response is delivered without caching (graceful
degradation).
Bug: Thundering herd / cache stampede
When many concurrent requests arrive just before a cached entry expires, all
of them miss and recompute simultaneously, overwhelming the origin.
Fix: Added
stampede_protection: bool = Falsetocache(). Whenenabled and the remaining TTL drops below 10% of the original TTL, a hit
is probabilistically promoted to a miss with probability
1 - (remaining_ttl / threshold). Only a fraction of concurrent requestsrecompute, keeping origin load manageable.