Guard against re-entrant ValkeyCache context use - #1080
Open
pctablet505 wants to merge 1 commit into
Open
Conversation
Nesting the same ValkeyCache instance in multiple `async with` blocks silently replaced `self.client` with a new GlideClient on each entry, so the outer block's exit closed the inner client (or double-closed it) while the original connection could leak. `__aenter__` now raises RuntimeError if a client is already active, and `__aexit__` resets `self.client` to None after closing so the instance can still be reused sequentially. Fixes aio-libs#1023.
pctablet505
marked this pull request as ready for review
July 17, 2026 12:47
Member
There was a problem hiding this comment.
Please use create_autospec(..., spec_set=True, instance=True) and patch(..., autospec=True, spec_set=True).
|
|
||
| async def __aexit__(self, *args, **kwargs) -> None: | ||
| await self.client.close() | ||
| if self.client is not None: |
Member
There was a problem hiding this comment.
This should be an assert, it should never be false.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1080 +/- ##
==========================================
- Coverage 98.85% 98.83% -0.02%
==========================================
Files 32 32
Lines 3579 3607 +28
Branches 125 129 +4
==========================================
+ Hits 3538 3565 +27
Misses 41 41
- Partials 0 1 +1
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
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.
What do these changes do?
ValkeyCache.__aenter__always created a brand newGlideClientandoverwrote
self.client, even if the instance was already inside anasync withblock. Nesting the same instance (e.g.async with cache: async with cache: ...) silently replaced the connection, so the innerblock's exit closed the newer client while the outer block's exit then
closed it again (or the original connection leaked, never being closed).
__aenter__now raisesRuntimeErrorif a client is already active,and
__aexit__clearsself.clientback toNoneafter closing sothe same instance can still be used again sequentially (just not
nested).
Are there changes in behavior for the user?
Nesting the same
ValkeyCacheinstance in overlappingasync withblocks now raises
RuntimeErrorinstead of silently leaking/double-closingconnections. Normal (non-nested) usage is unaffected.
Related issue number
Fixes #1023
Checklist