Open
Description
I think there might be an issue with the __aenter__
method on the ValkeyBackend.
aiocache/aiocache/backends/valkey.py
Lines 32 to 37 in d685fff
This code snippet explains my thinking.
async def cache_operations():
client_1 = None
async with ValkeyCache(
config=GlideClientConfiguration(addresses=[NodeAddress("localhost", 6379)])
) as my_cache_1: # <<< new client created
client_1 = my_cache_1.client
async with my_cache_1 as my_cache_2: # <<< new client created
assert my_cache_1 is my_cache_2 # because of return self
client_2 = my_cache_2
assert client_1 != client_2
assert my_cache_1.client == client_2
# exit inner with, call __aexit__, calls await self.client.close(); i.e. client_2.close())
assert my_cache_1.client != client_1 # it's still client_2
# exit inner with, call __aexit__, calls await self.client.close(); i.e. client_2.close() again, which may error.
# client_1.close() may never be called
I think that the __aenter__
method should maybe handle checking if an existing connection is available and disposing of it if not, or else return a clone of self
with a new client to ensure that the connection does get managed correctly.
Metadata
Metadata
Assignees
Labels
No labels