Skip to content

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]

[Fix] conftest: flush cache instances and warn on silent skips

Yuneng Jiang4b3f5d7f81d3
diff --git a/tests/proxy_unit_tests/conftest.py b/tests/proxy_unit_tests/conftest.py
index 544b6a0b421..a0326f64ed7 100644
--- a/tests/proxy_unit_tests/conftest.py
+++ b/tests/proxy_unit_tests/conftest.py
@@ -2,8 +2,10 @@
import asyncio
import copy
+import inspect
import os
import sys
+import warnings
import pytest
@@ -15,33 +17,34 @@ import litellm
import litellm.proxy.proxy_server
+# Top-level assignments of these types are the ones importlib.reload(litellm)
+# would have effectively reset. We snapshot them at conftest import time and
+# deep-copy the snapshot back before every test.
_SNAPSHOT_TYPES = (list, dict, set, tuple, str, int, float, bool, bytes)
def _snapshot_mutable_state(module):
- """Snapshot every module attribute that importlib.reload would have reset.
-
- Covers the top-level assignments that tests mutate — collections
- (callbacks, caches, general_settings) plus scalar flags (master_key,
- premium_user, etc.) that gate auth and feature behavior. Classes,
- functions, submodules and complex object instances are skipped: those
- either aren't meant to be reset or can't round-trip through deepcopy.
- """
+ """Capture a per-module snapshot of primitive and collection attributes."""
snapshot = {}
for attr in list(vars(module)):
if attr.startswith("_"):
continue
try:
value = getattr(module, attr)
- except Exception:
+ except Exception as exc:
+ warnings.warn(
+ f"conftest: could not read {module.__name__}.{attr} during snapshot: {exc}",
+ stacklevel=2,
+ )
continue
if value is None or isinstance(value, _SNAPSHOT_TYPES):
try:
snapshot[attr] = copy.deepcopy(value)
- except Exception:
- # Skip anything that can't round-trip through deepcopy
- # rather than crash collection.
- pass
+ except Exception as exc:
+ warnings.warn(
+ f"conftest: could not snapshot {module.__name__}.{attr}: {exc}",
+ stacklevel=2,
+ )
return snapshot
@@ -49,28 +52,84 @@ def _restore_mutable_state(module, snapshot):
for attr, default in snapshot.items():
try:
setattr(module, attr, copy.deepcopy(default))
+ except Exception as exc:
+ warnings.warn(
+ f"conftest: could not restore {module.__name__}.{attr}: {exc}",
+ stacklevel=2,
+ )
+
+
+def _collect_flushable_caches():
+ """Return (module, attr) pairs whose values expose flush_cache()."""
+ targets = []
+ for module in (litellm, litellm.proxy.proxy_server):
+ for attr in list(vars(module)):
+ if attr.startswith("_"):
+ continue
+ try:
+ value = getattr(module, attr)
+ except Exception:
+ continue
+ # Only instances — a class reference has an unbound flush_cache
+ # that can't be called without a self argument.
+ if inspect.isclass(value) or inspect.ismodule(value):
+ continue
+ if callable(getattr(value, "flush_cache", None)):
+ targets.append((module, attr))
+ return targets
+
+
+def _flush_caches(targets):
+ for module, attr in targets:
+ try:
+ value = getattr(module, attr)
except Exception:
- pass
+ continue
+ flush = getattr(value, "flush_cache", None)
+ if callable(flush):
+ try:
+ flush()
+ except Exception as exc:
+ warnings.warn(
+ f"conftest: flush_cache failed on {module.__name__}.{attr}: {exc}",
+ stacklevel=2,
+ )
# Snapshot once at conftest import — these are the "clean" module states.
_LITELLM_STATE = _snapshot_mutable_state(litellm)
_PROXY_SERVER_STATE = _snapshot_mutable_state(litellm.proxy.proxy_server)
+_FLUSHABLE_CACHES = _collect_flushable_caches()
@pytest.fixture(scope="function", autouse=True)
def setup_and_teardown():
- """
- Reset mutable module state on litellm and proxy_server before every test.
+ """Reset mutable module state on litellm and proxy_server before each test.
Replaces a previous importlib.reload(litellm) approach that cost ~17s
- per test (re-executing the full litellm __init__ import chain). The
- snapshot-and-restore below only touches collections that actually leak
- across tests — callbacks, caches, router, etc. — and is effectively
- instantaneous.
+ per test (re-executing the full litellm __init__ import chain).
+
+ What IS reset:
+ - Top-level module attributes of type list / dict / set / tuple
+ / str / int / float / bool / bytes, and None-valued attributes.
+ These cover callback lists, general_settings, master_key,
+ premium_user, prisma_client, etc. — anything the old reload() reset
+ by re-executing the module body.
+ - Any module-level object instance that exposes flush_cache() (the
+ DualCache and LLMClientCache family), which handles cache state
+ that can't round-trip through deepcopy because of internal locks.
+
+ What is NOT reset:
+ - Class instances without flush_cache() (e.g. ProxyLogging,
+ JWTHandler, FastAPI routers, loggers). If a test mutates such an
+ instance in-place (setattr on the instance, appending to one of
+ its internal lists, etc.), the mutation will leak into later tests.
+ Use pytest's monkeypatch.setattr() or a local fixture for those
+ cases — don't rely on this autouse fixture to undo them.
"""
_restore_mutable_state(litellm, _LITELLM_STATE)
_restore_mutable_state(litellm.proxy.proxy_server, _PROXY_SERVER_STATE)
+ _flush_caches(_FLUSHABLE_CACHES)
loop = asyncio.get_event_loop_policy().new_event_loop()
asyncio.set_event_loop(loop)