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
Add memory leak detection tests with CI integration (#18881)
Alexsander Hamir578abd4465d0
diff --git a/.circleci/config.yml b/.circleci/config.ymlindex 476f138b1d4..f9d2293f1ac 100644--- a/.circleci/config.yml+++ b/.circleci/config.yml@@ -2038,6 +2038,39 @@ jobs:- run: python ./tests/code_coverage_tests/memory_test.py- run: helm lint ./deploy/charts/litellm-helm+ memory_leak_tests:+ docker:+ - image: cimg/python:3.11+ auth:+ username: ${DOCKERHUB_USERNAME}+ password: ${DOCKERHUB_PASSWORD}+ working_directory: ~/project+ resource_class: large+ steps:+ - setup_litellm_test_deps+ - run:+ name: Install Memory Test Dependencies+ command: |+ pip install "psutil>=5.9.0"+ pip install "fastapi>=0.100.0"+ pip install "httpx>=0.24.0"+ pip install "uvicorn>=0.23.0"+ - run:+ name: Run Linear Memory Growth Tests+ command: |+ echo "Running memory leak tests individually to avoid baseline drift..."+ echo "Running test_memory_baseline_1k..."+ python -m pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_1k -v -s --tb=short+ echo "Running test_memory_baseline_2k..."+ python -m pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_2k -v -s --tb=short+ echo "Running test_memory_baseline_4k..."+ python -m pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_4k -v -s --tb=short+ echo "Running test_memory_baseline_10k..."+ python -m pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_10k -v -s --tb=short+ echo "Running test_memory_baseline_30k..."+ python -m pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_30k -v -s --tb=short+ no_output_timeout: 60m+db_migration_disable_update_check:machine:image: ubuntu-2204:2023.10.1@@ -3765,6 +3798,12 @@ workflows:only:- main- /litellm_.*/+ - memory_leak_tests:+ filters:+ branches:+ only:+ - main+ - /litellm_.*/- ui_build:filters:branches:diff --git a/pyproject.toml b/pyproject.tomlindex 81fa12fef76..03a73f38e2d 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -179,4 +179,14 @@ plugins = "pydantic.mypy"asyncio_mode = "auto"markers = ["asyncio: mark test as an asyncio test",+ "limit_leaks: mark test with memory limit for leak detection (e.g., '40 MB')",+ "no_parallel: mark test to run sequentially (not in parallel) - typically for memory measurement tests",+]+filterwarnings = [+ # Suppress Pydantic serializer warnings from mock server responses (non-critical for memory tests)+ # These occur because the mock server returns a simplified response format+ "ignore:Pydantic serializer warnings:UserWarning",+ "ignore::UserWarning:pydantic.main",+ # Suppress pytest-asyncio event loop deprecation warning (handled automatically by pytest-asyncio)+ "ignore::DeprecationWarning:pytest_asyncio.plugin",]diff --git a/tests/load_tests/memory_leak_utils.py b/tests/load_tests/memory_leak_utils.pynew file mode 100644index 00000000000..160a67fa184--- /dev/null+++ b/tests/load_tests/memory_leak_utils.py@@ -0,0 +1,314 @@+"""+Memory Leak Testing Utilities++This module provides reusable utilities, fixtures, and helpers for memory leak+and OOM (Out of Memory) detection tests. It includes:+- Mock server setup for local testing+- Memory tracking fixtures+- Router fixtures configured for testing+- Helper functions for running memory baseline tests++Usage:+ from tests.load_tests.memory_leak_utils import (+ mock_server,+ limit_memory,+ test_router,+ run_memory_baseline_test,+ )+"""++import gc+import os+import socket+import sys+import time+from threading import Thread++# Add parent directory to path to import litellm (same pattern as other tests)+filepath = os.path.dirname(os.path.abspath(__file__))+sys.path.insert(0, os.path.abspath(os.path.join(filepath, "../..")))++import httpx+import pytest+import psutil+from fastapi import FastAPI, Request+from fastapi.responses import JSONResponse+from litellm.router import Router++# Test Configuration Constants+TEST_API_KEY = "sk-1234"+TEST_MODEL_NAME = "gpt-3.5-turbo"++# Timing Constants (seconds)+GC_STABILIZATION_DELAY = 0.05+++# Mock OpenAI-compatible server+def create_mock_server():+ """Create a simple FastAPI mock server that mimics OpenAI API responses."""+ app = FastAPI()++ @app.post("/v1/chat/completions")+ @app.post("/chat/completions")+ async def chat_completions(request: Request):+ """Mock OpenAI chat completions endpoint."""+ request_data = await request.json()+ # Return a simple mock response+ return JSONResponse({+ "id": "chatcmpl-mock",+ "object": "chat.completion",+ "created": int(time.time()),+ "model": request_data.get("model", TEST_MODEL_NAME),+ "choices": [{+ "index": 0,+ "message": {+ "role": "assistant",+ "content": "Mock response"+ },+ "finish_reason": "stop"+ }],+ "usage": {+ "prompt_tokens": 10,+ "completion_tokens": 5,+ "total_tokens": 15+ }+ })++ # Catch-all route to see what URLs are being requested+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])+ async def catch_all(request: Request, path: str):+ """Catch-all route to debug what URLs are being requested."""+ print(f"[Mock Server] Received request: {request.method} {request.url.path}")+ # For non-chat-completions, return 404+ return JSONResponse({"detail": "Not Found"}, status_code=404)++ return app+++def run_server(app, port):+ """Run uvicorn server in a thread."""+ import uvicorn+ # Use uvicorn.run which blocks - this is fine in a daemon thread+ uvicorn.run(app, host="127.0.0.1", port=port, log_level="error", access_log=False)++[email protected](scope="session")+def mock_server():+ """Start a mock server in a separate thread for the test session.++ Yields the server URL (with trailing slash) for use in router configuration.+ """+ app = create_mock_server()+ port = 18888++ # Check if port is already in use+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)+ try:+ sock.bind(("127.0.0.1", port))+ sock.close()+ except OSError:+ # Port already in use, try next port+ port = 18889+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)+ try:+ sock.bind(("127.0.0.1", port))+ sock.close()+ except OSError:+ pytest.fail(f"Could not find available port for mock server (tried 18888, 18889)")++ # Start server in background thread+ thread = Thread(target=lambda: run_server(app, port), daemon=True)+ thread.start()++ # Wait for server to start and verify it's accessible+ # Ensure api_base has trailing slash (LiteLLM appends /v1/chat/completions)+ server_url = f"http://127.0.0.1:{port}/"+ max_attempts = 20 # More attempts to ensure server is ready+ server_ready = False+ for attempt in range(max_attempts):+ time.sleep(0.3) # Longer wait between attempts+ try:+ # Test the actual endpoint we'll use (LiteLLM appends /v1/chat/completions to api_base)+ response = httpx.post(+ f"{server_url}v1/chat/completions",+ json={"model": TEST_MODEL_NAME, "messages": [{"role": "user", "content": "test"}]},+ timeout=2.0+ )+ if response.status_code == 200:+ server_ready = True+ print(f"[Mock Server] Server ready at {server_url}")+ break+ except (httpx.ConnectError, httpx.TimeoutException, httpx.NetworkError) as e:+ # Server not ready yet, continue waiting+ if attempt == max_attempts - 1:+ pytest.fail(+ f"Mock server failed to start on {server_url} after {max_attempts} attempts. "+ f"Could not connect to /v1/chat/completions endpoint. Error: {e}"+ )+ continue+ except Exception as e:+ # Other errors might indicate server is up but endpoint has issues+ # If we get a response (even error), server is running+ print(f"[Mock Server] Server responded with error (but is running): {e}")+ server_ready = True+ break++ if not server_ready:+ pytest.fail(f"Mock server not accessible at {server_url} after {max_attempts} attempts")++ yield server_url++ # Server will be cleaned up when thread dies (daemon=True)+++def limit_memory(request):+ """Fixture to track memory usage and enforce limits via @pytest.mark.limit_leaks marker.++ Usage:+ @pytest.mark.limit_leaks("40 MB")+ def test_something(limit_memory):+ # Test code here+ # Memory will be measured and test will fail if increase exceeds limit+ """+ marker = request.node.get_closest_marker("limit_leaks")+ if marker:+ # Parse limit from marker (e.g., "30 MB" -> 30)+ limit_str = marker.args[0] if marker.args else "100 MB"+ limit_mb = float(limit_str.split()[0])+ limit_bytes = limit_mb * 1024 * 1024++ # Measure baseline memory (router will be fresh from fixture)+ process = psutil.Process(os.getpid())+ baseline_memory = process.memory_info().rss++ yield++ # Force GC before measuring final memory+ gc.collect()+ # Small delay for memory to stabilize+ time.sleep(GC_STABILIZATION_DELAY)++ # Measure final memory after test+ final_memory = process.memory_info().rss+ memory_increase = final_memory - baseline_memory+ memory_increase_mb = memory_increase / 1024 / 1024++ # Print memory stats+ print(f"\n[Memory Limit Test] Memory usage:")+ print(f" Baseline: {baseline_memory / 1024 / 1024:.2f} MB")+ print(f" Final: {final_memory / 1024 / 1024:.2f} MB")+ print(f" Increase: {memory_increase_mb:+.2f} MB")+ print(f" Limit: {limit_mb:.2f} MB")++ # Fail if memory increase exceeds limit+ if memory_increase > limit_bytes:+ pytest.fail(+ f"Memory limit exceeded: {memory_increase_mb:.2f} MB increase > {limit_mb:.2f} MB limit. "+ f"Baseline: {baseline_memory / 1024 / 1024:.2f} MB, Final: {final_memory / 1024 / 1024:.2f} MB"+ )+ else:+ yield+++def test_router(mock_server):+ """Fixture to create a fresh router instance for each test.++ Uses the mock server fixture to avoid external API calls.+ Disables cooldowns to prevent deployments from being marked unavailable.++ Usage:+ def test_something(test_router, limit_memory):+ # Use test_router for making requests+ response = await test_router.acompletion(...)+ """+ router = Router(+ model_list=[+ {+ "model_name": TEST_MODEL_NAME,+ "litellm_params": {+ "model": f"openai/{TEST_MODEL_NAME}",+ "api_base": mock_server,+ "api_key": TEST_API_KEY,+ },+ },+ ],+ disable_cooldowns=True, # Disable cooldowns for testing+ allowed_fails=1000, # Allow many failures before cooldown (effectively disabled)+ )+ yield router+ # Cleanup after test+ try:+ router.discard()+ except Exception:+ pass # Ignore cleanup errors+++async def run_memory_baseline_test(num_requests: int, router: Router, limit_memory):+ """Helper function to run memory baseline test with specified number of requests.++ Makes requests concurrently in batches for speed, with proper error handling+ that doesn't fail the test on individual request failures.++ Args:+ num_requests: Number of requests to make.+ router: Router instance to use for requests.+ limit_memory: Pytest fixture for memory tracking (reference to suppress linter warning).++ Example:+ @pytest.mark.asyncio+ @pytest.mark.limit_leaks("40 MB")+ async def test_memory(test_router, limit_memory):+ await run_memory_baseline_test(1000, test_router, limit_memory)+ """+ # Fixture is used automatically by pytest - reference it to suppress linter warning+ _ = limit_memory++ # Make requests concurrently in batches for speed+ # Batch size of 20 provides good balance between speed and memory pressure+ BATCH_SIZE = 20++ for batch_start in range(0, num_requests, BATCH_SIZE):+ batch_end = min(batch_start + BATCH_SIZE, num_requests)+ # Create concurrent tasks for this batch+ tasks = [+ router.acompletion(+ model=TEST_MODEL_NAME,+ messages=[{"role": "user", "content": f"Test request {i}"}],+ )+ for i in range(batch_start, batch_end)+ ]+ # Execute batch concurrently+ # Note: return_exceptions=True allows test to continue even if some requests fail+ import asyncio+ responses = await asyncio.gather(*tasks, return_exceptions=True)+ # Filter out failed requests but continue with test+ valid_responses = []+ failed_count = 0+ for i, response in enumerate(responses):+ if isinstance(response, Exception):+ failed_count += 1+ # Log exception but continue+ print(f" Warning: Request {batch_start + i} failed: {type(response).__name__}: {response}")+ elif response is None:+ failed_count += 1+ print(f" Warning: Request {batch_start + i} returned None")+ else:+ valid_responses.append(response)++ # Continue with valid responses - don't fail the test+ # If all failed, that's logged but test continues (might indicate bigger issue)+ if failed_count > 0:+ print(f" Note: {failed_count}/{len(responses)} requests failed in batch {batch_start}-{batch_end}, continuing with {len(valid_responses)} valid responses")++ # Use valid_responses for cleanup+ responses = valid_responses+ # Clean up batch+ del responses+ del tasks+ del valid_responses+ # GC after each batch to prevent accumulation+ gc.collect()++ print(f"[Simple Memory Test] Completed {num_requests} requests")diff --git a/tests/load_tests/test_linear_memory_growth.py b/tests/load_tests/test_linear_memory_growth.pynew file mode 100644index 00000000000..3b7b8041b90--- /dev/null+++ b/tests/load_tests/test_linear_memory_growth.py@@ -0,0 +1,121 @@+"""+Memory Leak Detection Tests - Linear Memory Growth++Tests that check for linear/progressive memory growth by running different numbers+of requests (1k, 2k, 4k, 10k, 30k) with the same memory limit. If lower request+count tests pass but higher ones fail, it indicates linear memory growth per request.++These tests will fail if memory leaks are detected, helping catch OOM issues before production.++IMPORTANT: These tests should be run INDIVIDUALLY, not all together. Running them+together causes memory baseline drift between tests, making it difficult to detect+linear growth accurately. Each test should be run in isolation:++ pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_1k -v+ pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_2k -v+ # etc.++NOTE: Not recommended for accurate results:+pytest tests/load_tests/test_linear_memory_growth.py -v+"""++import pytest++from tests.load_tests.memory_leak_utils import (+ limit_memory, # noqa: F401 # pytest fixture used via dependency injection+ mock_server, # noqa: F401 # pytest fixture used via dependency injection+ run_memory_baseline_test,+ test_router, # noqa: F401 # pytest fixture used via dependency injection+)++# Memory limit for all linear memory growth tests+MEMORY_LIMIT = "40 MB"++[email protected]_leaks(MEMORY_LIMIT)[email protected]_parallel # Must run sequentially - measures process memory+async def test_memory_baseline_1k(test_router, limit_memory):+ """+ Memory baseline test with 1,000 requests.+ Uses @pytest.mark.limit_leaks("40 MB") to enforce memory limit.+ If this passes but higher request count tests fail, indicates progressive memory leak.++ NOTE: This test should be run INDIVIDUALLY, not with other tests in this file.+ Running multiple tests together causes memory baseline drift, making it difficult+ to accurately detect linear memory growth. Run with:+ pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_1k -v+ """+ await run_memory_baseline_test(1000, test_router, limit_memory)++[email protected]_leaks(MEMORY_LIMIT)[email protected]_parallel # Must run sequentially - measures process memory+async def test_memory_baseline_2k(test_router, limit_memory):+ """+ Memory baseline test with 2,000 requests.+ Uses @pytest.mark.limit_leaks("40 MB") to enforce memory limit.+ If this passes but test_memory_baseline_4k fails, indicates progressive memory leak.++ NOTE: This test should be run INDIVIDUALLY, not with other tests in this file.+ Running multiple tests together causes memory baseline drift, making it difficult+ to accurately detect linear memory growth. Run with:+ pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_2k -v+ """+ await run_memory_baseline_test(2000, test_router, limit_memory)++[email protected]_leaks(MEMORY_LIMIT)[email protected]_parallel # Must run sequentially - measures process memory+async def test_memory_baseline_4k(test_router, limit_memory):+ """+ Memory baseline test with 4,000 requests.+ Uses @pytest.mark.limit_leaks("40 MB") to enforce memory limit.+ If test_memory_baseline_1k and test_memory_baseline_2k pass but this fails,+ it's a clear sign of sequential/progressive memory growth.++ NOTE: This test should be run INDIVIDUALLY, not with other tests in this file.+ Running multiple tests together causes memory baseline drift, making it difficult+ to accurately detect linear memory growth. Run with:+ pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_4k -v+ """+ await run_memory_baseline_test(4000, test_router, limit_memory)+++[email protected]_leaks(MEMORY_LIMIT)[email protected]_parallel # Must run sequentially - measures process memory+async def test_memory_baseline_10k(test_router, limit_memory):+ """+ Memory baseline test with 10,000 requests.+ Uses @pytest.mark.limit_leaks("40 MB") to enforce memory limit.+ If test_memory_baseline_1k and test_memory_baseline_2k pass but this fails,+ it's a clear sign of sequential/progressive memory growth.++ NOTE: This test should be run INDIVIDUALLY, not with other tests in this file.+ Running multiple tests together causes memory baseline drift, making it difficult+ to accurately detect linear memory growth. Run with:+ pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_10k -v+ """+ await run_memory_baseline_test(10000, test_router, limit_memory)++[email protected]_leaks(MEMORY_LIMIT)[email protected]_parallel # Must run sequentially - measures process memory+async def test_memory_baseline_30k(test_router, limit_memory):+ """+ Memory baseline test with 30,000 requests.+ Uses @pytest.mark.limit_leaks("40 MB") to enforce memory limit.+ If test_memory_baseline_1k and test_memory_baseline_2k pass but this fails,+ it's a clear sign of sequential/progressive memory growth.++ NOTE: This test should be run INDIVIDUALLY, not with other tests in this file.+ Running multiple tests together causes memory baseline drift, making it difficult+ to accurately detect linear memory growth. Run with:+ pytest tests/load_tests/test_linear_memory_growth.py::test_memory_baseline_30k -v+ """+ await run_memory_baseline_test(30000, test_router, limit_memory)