The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): stop cache eviction errors from failing /key/update
Yuneng Jiangd8ad578045ae
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.pyindex 366950c00a1..66bbda1ca4e 100644--- a/litellm/proxy/auth/auth_checks.py+++ b/litellm/proxy/auth/auth_checks.py@@ -2588,13 +2588,29 @@ async def _delete_cache_key_object(user_api_key_cache: UserApiKeyCache,proxy_logging_obj: ProxyLogging | None,):+ """+ Evict one key object, best-effort, matching `delete_cache_team_object` and+ `delete_cache_key_objects`.++ Every caller runs this after its own write has already committed, and the in-memory entry is+ dropped before the Redis round trip. Letting a cache-backend error raise here therefore reports+ failure for work that succeeded without making the cache any less stale; the leftover Redis+ entry expires at its TTL either way.+ """key: Final = hashed_token- user_api_key_cache.delete_cache(key=key)+ try:+ user_api_key_cache.delete_cache(key=key)- ## UPDATE REDIS CACHE ##- if proxy_logging_obj is not None:- await proxy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache(key=key)+ ## UPDATE REDIS CACHE ##+ if proxy_logging_obj is not None:+ await proxy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache(key=key)+ except Exception as e: # noqa: BLE001 # best-effort: a cache error must not fail a committed write+ verbose_proxy_logger.warning(+ "Failed to invalidate cached key entry %s; a stale key object may be served until its TTL expires: %s",+ key,+ e,+ )async def delete_cache_key_objects(diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.pyindex 7d2e3f45c04..3dea89ed67b 100644--- a/tests/test_litellm/proxy/auth/test_auth_checks.py+++ b/tests/test_litellm/proxy/auth/test_auth_checks.py@@ -7394,3 +7394,57 @@ async def test_invalidate_team_member_spend_state_self_delivered_broadcast_does_assert (local_spend_counter_cache.in_memory_cache.get_cache("spend_db_floor:spend:team_member:user-1:team-1") == 0.0), "the handler's self-delivered broadcast erased the post-reset floor marker, reopening the stale-floor race"+++async def test_delete_cache_key_object_is_best_effort_when_the_cache_backend_fails(caplog):+ """+ LIT-5898: `_delete_cache_key_object` must not propagate a cache-backend error.++ Every caller runs it after its own write has committed, so a raise here turned a persisted+ `/key/update` into `400 Authentication Error` (and `/key/block`, `/key/regenerate` into 500s)+ for operators whose Redis ACL denies `DEL` on LiteLLM's unprefixed token-hash keys. The+ in-memory entry is already dropped by then, so raising never made the cache less stale.+ """+ import logging+ from unittest.mock import AsyncMock, MagicMock++ from litellm.proxy.auth.auth_checks import _delete_cache_key_object++ hashed_token = "a" * 64+ caplog.set_level(logging.WARNING, logger="LiteLLM Proxy")++ failing_cache = MagicMock()+ failing_cache.delete_cache = MagicMock()+ failing_logging_obj = MagicMock()+ failing_logging_obj.internal_usage_cache.dual_cache.async_delete_cache = AsyncMock(+ side_effect=Exception("No permissions to access a key")+ )++ await _delete_cache_key_object(+ hashed_token=hashed_token,+ user_api_key_cache=failing_cache,+ proxy_logging_obj=failing_logging_obj,+ )++ failing_cache.delete_cache.assert_called_once_with(key=hashed_token)+ failing_logging_obj.internal_usage_cache.dual_cache.async_delete_cache.assert_awaited_once_with(key=hashed_token)+ assert any("Failed to invalidate cached key entry" in record.getMessage() for record in caplog.records), (+ "a swallowed cache-eviction failure must still be logged, or a stale auth entry goes unnoticed"+ )++ caplog.clear()+ healthy_cache = MagicMock()+ healthy_cache.delete_cache = MagicMock()+ healthy_logging_obj = MagicMock()+ healthy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache = AsyncMock()++ await _delete_cache_key_object(+ hashed_token=hashed_token,+ user_api_key_cache=healthy_cache,+ proxy_logging_obj=healthy_logging_obj,+ )++ healthy_cache.delete_cache.assert_called_once_with(key=hashed_token)+ healthy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache.assert_awaited_once_with(key=hashed_token)+ assert caplog.records == [], "a healthy eviction must stay silent, and must still reach both caches"