fix(redis): guard missing async pool on shutdown

This commit is contained in:
adhavan18 2026-09-24 14:11:57 +05:30
parent 518800cd57
commit 5e843bcfde
2 changed files with 29 additions and 1 deletions

View file

@ -1785,7 +1785,8 @@ class RedisCache(BaseCache):
self.redis_client.flushall()
async def disconnect(self):
await self.async_redis_conn_pool.disconnect(inuse_connections=True)
if self.async_redis_conn_pool is not None:
await self.async_redis_conn_pool.disconnect(inuse_connections=True)
try:
self.redis_client.close()
except Exception as e:

View file

@ -0,0 +1,27 @@
from unittest.mock import AsyncMock, Mock
import pytest
from litellm.caching.redis_cache import RedisCache
@pytest.mark.asyncio
async def test_disconnect_allows_missing_async_pool():
cache = RedisCache.__new__(RedisCache)
cache.async_redis_conn_pool = None
cache.redis_client = Mock()
await cache.disconnect()
cache.redis_client.close.assert_called_once_with()
@pytest.mark.asyncio
async def test_disconnect_closes_present_async_pool():
cache = RedisCache.__new__(RedisCache)
cache.async_redis_conn_pool = Mock(disconnect=AsyncMock())
cache.redis_client = Mock()
await cache.disconnect()
cache.async_redis_conn_pool.disconnect.assert_awaited_once_with(inuse_connections=True)