fix: base64 body encoding and client close in cache layer - #41
Open
emiliano-go wants to merge 2 commits into
Open
emiliano-go wants to merge 2 commits into
emiliano-go wants to merge 2 commits into
Conversation
- M1: Use base64 encoding for response body in cache entries instead of lossy decode(errors='replace'), with backward-compatible read path - L5: Make _PoolState.clear() async and close the AsyncRedis client before resetting state to prevent connection leaks - H4: Widen except clause from (json.JSONDecodeError, KeyError) to Exception in cache-hit path - Add tests for base64 round-trip, pool client close, and update existing _PoolState test to async
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #41 +/- ##
==========================================
- Coverage 96.81% 96.75% -0.07%
==========================================
Files 12 12
Lines 1257 1262 +5
==========================================
+ Hits 1217 1221 +4
- Misses 40 41 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- Add test_build_hit_response_decodes_base64_body to directly exercise the base64 decode path in _build_hit_response (line 313) - Add test_build_hit_response_304_not_modified to cover the 304 path - Resolves codecov missing coverage on PR redis#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.
Summary
Fixes two cache layer issues: lossy binary body encoding and connection leak on shutdown.
Changes
Binary-safe body encoding
_write_cache_entrystored response bodies usingdecode(errors="replace"). This silently corrupts binary responses (images, protobuf, msgpack). The replacement character makes round-trip lossy.Now uses base64 encoding with an "encoding" field in the cache entry. The read path checks for this field. Entries without it use the legacy str/encode path for backward compatibility.
Close client on pool state clear
_PoolState.clear()set_async_client = Nonewithout callingaclose(). This leaks the connection pool on shutdown.Now
clear()is async and callsaclose()before resetting state. The lifespan handler awaits it.Files changed
src/redis_fastapi/cache.py: base64 encode body in write path, decode in read path based on encoding fieldsrc/redis_fastapi/deps.py: make_PoolState.clear()async, callaclose()src/redis_fastapi/lifespan.py: awaitps.clear()tests/unit/test_adversarial.py: 3 new tests, update existing_PoolStatetest to asyncTest results
nox passes across Python 3.10-3.14. 407 tests, 93.42% coverage.
Related