Skip to content

Guard against re-entrant ValkeyCache context use - #1080

Open
pctablet505 wants to merge 1 commit into
aio-libs:masterfrom
pctablet505:fix-valkey-reentrant-context
Open

Guard against re-entrant ValkeyCache context use#1080
pctablet505 wants to merge 1 commit into
aio-libs:masterfrom
pctablet505:fix-valkey-reentrant-context

Conversation

@pctablet505

Copy link
Copy Markdown

What do these changes do?

ValkeyCache.__aenter__ always created a brand new GlideClient and
overwrote self.client, even if the instance was already inside an
async with block. Nesting the same instance (e.g. async with cache: async with cache: ...) silently replaced the connection, so the inner
block'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 raises RuntimeError if a client is already active,
and __aexit__ clears self.client back to None after closing so
the same instance can still be used again sequentially (just not
nested).

Are there changes in behavior for the user?

Nesting the same ValkeyCache instance in overlapping async with
blocks now raises RuntimeError instead of silently leaking/double-closing
connections. Normal (non-nested) usage is unaffected.

Related issue number

Fixes #1023

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

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
pctablet505 marked this pull request as ready for review July 17, 2026 12:47

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an assert, it should never be false.

@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.83%. Comparing base (ae5948b) to head (d6003f7).

Files with missing lines Patch % Lines
aiocache/backends/valkey.py 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
tests/ut/backends/test_valkey.py 100.00% <100.00%> (ø)
aiocache/backends/valkey.py 99.13% <85.71%> (-0.87%) ⬇️

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ae5948b...d6003f7. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential memory leak/bug in the valkey client

2 participants