This commit is contained in:
tsushanth 2026-08-27 20:00:44 -07:00 committed by GitHub
commit cd7650e3f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 0 deletions

View file

@ -1370,6 +1370,8 @@ class RedisCache(BaseCache):
self.redis_client.flushall()
async def disconnect(self):
if self.async_redis_conn_pool is None:
return
await self.async_redis_conn_pool.disconnect(inuse_connections=True)
try:
self.redis_client.close()

View file

@ -56,6 +56,16 @@ class RedisClusterCache(RedisCache):
async_redis_cluster_client: Final = self.init_async_client()
return await async_redis_cluster_client.mget_nonatomic(keys=keys)
async def disconnect(self):
if self.redis_async_redis_cluster_client is not None:
await self.redis_async_redis_cluster_client.aclose()
try:
self.redis_client.close()
except Exception as e:
from litellm._logging import verbose_logger
verbose_logger.debug("Error closing sync Redis Cluster client: %s", e)
async def test_connection(self) -> dict:
"""
Test the Redis Cluster connection.

View file

@ -464,6 +464,17 @@ def test_delete_cache_namespaces_key(namespace, expected, monkeypatch, redis_no_
redis_cache.delete_cache(key="k")
mock_client.delete.assert_called_once_with(expected)
@pytest.mark.asyncio
async def test_disconnect_with_none_conn_pool():
"""Regression test: disconnect() must not raise AttributeError when async_redis_conn_pool is None (cluster mode)."""
with patch("asyncio.get_running_loop", side_effect=RuntimeError):
cache = RedisCache(host="localhost", port=6379, password="x")
cache.async_redis_conn_pool = None
# Should return without raising
await cache.disconnect()
def _closed_port() -> int:
"""A port with nothing listening, so Redis calls fail fast and deterministically."""