litellm/tests/test_litellm/caching
Cursor Agent a06404451d fix: prevent httpx client closed errors from cache eviction
Root cause: LLMClientCache._remove_key() was closing httpx clients when
they were evicted from cache (TTL or size-based). But other code still
held references to those clients (e.g., litellm.module_level_aclient
stored in module __dict__, in-flight requests). This caused
RuntimeError('Cannot send a request, as the client has been closed.')
for all subsequent users of those client references.

The issue manifested after ~1 hour (TTL=3600s) of uptime, as clients
were evicted from the LLMClientCache. It persisted until pod restart
because litellm.module_level_aclient in the module __dict__ was never
re-created (Python's __getattr__ is not called when the attribute
already exists in __dict__).

Fix (3 layers of defense):
1. LLMClientCache._remove_key() no longer closes clients on eviction.
   Client cleanup is deferred to GC (__del__) and atexit handlers,
   which only run when no references remain.
2. get_async_httpx_client() and _get_httpx_client() now check if a
   cached client's underlying httpx client is closed before returning
   it. If closed, they create a new one.
3. AsyncHTTPHandler.post/put/patch/delete/get catch RuntimeError with
   'client has been closed' and transparently retry with a fresh
   httpx.AsyncClient.

Co-authored-by: Ishaan Jaff <ishaan-jaff@users.noreply.github.com>
2026-02-26 17:54:50 +00:00
..
test_azure_blob_cache.py add azure blob cache support (#12587) 2025-07-15 11:47:38 -07:00
test_caching_handler.py Helicone base url support + fix for embedding cache hits on str input (#11211) 2025-05-28 22:02:55 -07:00
test_dual_cache.py fix(cache): prevent DualCache async batch check-then-act race (#20986) 2026-02-13 18:32:41 +05:30
test_gcs_cache.py Add GCS bucket caching support (#13122) 2025-08-04 16:09:33 -07:00
test_in_memory_cache.py [Performance] Reduce complexity of InMemoryCache.evict_cache from O(n*log(n)) to O(log(n)) (#15000) 2025-09-30 16:49:35 -07:00
test_qdrant_semantic_cache.py feat(semantic-cache): support configurable vector dimensions for Qdrant (#21649) 2026-02-21 00:51:15 -08:00
test_redis_cache.py fix(proxy): add LPOP pipeline error checking and fix org spend ServiceType 2026-02-24 14:22:57 -08:00
test_redis_cluster_cache.py Litellm fix GitHub action testing (#11163) 2025-05-26 14:41:42 -07:00
test_redis_connection_pool.py fix: prevent httpx client closed errors from cache eviction 2026-02-26 17:54:50 +00:00
test_redis_semantic_cache.py Litellm fix GitHub action testing (#11163) 2025-05-26 14:41:42 -07:00
test_s3_cache.py verify expires field prior to serving cache entry 2025-08-25 10:42:12 +02:00