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 semgrep & Fix OOMs (#20912)
Alexsander Hamirb7993b14cf4b
diff --git a/.circleci/config.yml b/.circleci/config.ymlindex 45d6b665900..6c7bbddb9f1 100644--- a/.circleci/config.yml+++ b/.circleci/config.yml@@ -112,6 +112,24 @@ jobs:python -m mypy .cd ..no_output_timeout: 10m++ semgrep:+ docker:+ - image: cimg/python:3.12+ auth:+ username: ${DOCKERHUB_USERNAME}+ password: ${DOCKERHUB_PASSWORD}+ working_directory: ~/project+ steps:+ - checkout+ - setup_google_dns+ - run:+ name: Install Semgrep+ command: pip install semgrep+ - run:+ name: Run Semgrep (custom rules only)+ command: semgrep scan --config .semgrep/rules . --error+local_testing_part1:docker:- image: cimg/python:3.12@@ -4118,6 +4136,12 @@ workflows:only:- main- /litellm_.*/+ - semgrep:+ filters:+ branches:+ only:+ - main+ - /litellm_.*/- local_testing_part1:filters:branches:@@ -4510,6 +4534,7 @@ workflows:- publish_to_pypi:requires:- mypy_linting+ - semgrep- local_testing_part1- local_testing_part2- build_and_testdiff --git a/.semgrep/rules/README.md b/.semgrep/rules/README.mdnew file mode 100644index 00000000000..0dbb77cdd48--- /dev/null+++ b/.semgrep/rules/README.md@@ -0,0 +1,22 @@+# Custom Semgrep rules for LiteLLM++Add custom rule YAML files here. Semgrep loads all `.yml`/`.yaml` files under this directory.++**Run only custom rules (CI / fail on findings):**++```bash+semgrep scan --config .semgrep/rules . --error+```++**Run with registry + custom rules:**++```bash+semgrep scan --config auto --config .semgrep/rules .+```++**Layout:**++- `python/` – Python-specific rules (security, patterns)+- Add more subdirs as needed (e.g. `generic/` for language-agnostic rules)++See [Semgrep rule syntax](https://semgrep.dev/docs/writing-rules/rule-syntax/).diff --git a/.semgrep/rules/python/unbounded-memory.yml b/.semgrep/rules/python/unbounded-memory.ymlnew file mode 100644index 00000000000..811ef689344--- /dev/null+++ b/.semgrep/rules/python/unbounded-memory.yml@@ -0,0 +1,14 @@+# Unbounded memory growth – data structures without a clear max limit+# Can lead to OOM under load.++rules:+ - id: unbounded-asyncio-queue+ message: asyncio.Queue() with no maxsize can grow unbounded. Use asyncio.Queue(maxsize=N) for integrations (e.g. log queues).+ severity: ERROR+ languages: [python]+ pattern-either:+ - pattern: asyncio.Queue()+ - pattern: asyncio.Queue(maxsize=0)+ metadata:+ category: correctness+ cwe: "CWE-400: Uncontrolled Resource Consumption"\ No newline at end of filediff --git a/cookbook/nova_sonic_realtime.py b/cookbook/nova_sonic_realtime.pyindex 0ea0badfb01..c7a73c1d00f 100644--- a/cookbook/nova_sonic_realtime.py+++ b/cookbook/nova_sonic_realtime.py@@ -16,10 +16,14 @@ Usage:import asyncioimport base64import json+import osimport pyaudioimport websocketsfrom typing import Optional+# Bounded queue size for audio chunks (configurable via env to avoid unbounded memory)+AUDIO_QUEUE_MAXSIZE = int(os.getenv("LITELLM_ASYNCIO_QUEUE_MAXSIZE", 10_000))+# Audio configuration (matching Nova Sonic requirements)INPUT_SAMPLE_RATE = 16000 # Nova Sonic expects 16kHz inputOUTPUT_SAMPLE_RATE = 24000 # Nova Sonic outputs 24kHz@@ -40,7 +44,7 @@ class RealtimeClient:self.api_key = api_keyself.ws: Optional[websockets.WebSocketClientProtocol] = Noneself.is_active = False- self.audio_queue = asyncio.Queue()+ self.audio_queue = asyncio.Queue(maxsize=AUDIO_QUEUE_MAXSIZE)self.pyaudio = pyaudio.PyAudio()self.input_stream = Noneself.output_stream = Nonediff --git a/docs/my-website/docs/proxy/config_settings.md b/docs/my-website/docs/proxy/config_settings.mdindex 5b6c6669b91..52504faede8 100644--- a/docs/my-website/docs/proxy/config_settings.md+++ b/docs/my-website/docs/proxy/config_settings.md@@ -784,6 +784,7 @@ router_settings:| LITELLM_USER_AGENT | Custom user agent string for LiteLLM API requests. Used for partner telemetry attribution| LITELLM_PRINT_STANDARD_LOGGING_PAYLOAD | If true, prints the standard logging payload to the console - useful for debugging| LITELM_ENVIRONMENT | Environment for LiteLLM Instance. This is currently only logged to DeepEval to determine the environment for DeepEval integration.+| LITELLM_ASYNCIO_QUEUE_MAXSIZE | Maximum size for asyncio queues (e.g. log queues, spend update queues, and cookbook examples such as realtime audio in `nova_sonic_realtime.py`). Bounds in-memory growth to prevent OOM. Default is 1000.| LOGFIRE_TOKEN | Token for Logfire logging service| LOGFIRE_BASE_URL | Base URL for Logfire logging service (useful for self hosted deployments)| LOGGING_WORKER_CONCURRENCY | Maximum number of concurrent coroutine slots for the logging worker on the asyncio event loop. Default is 100. Setting too high will flood the event loop with logging tasks which will lower the overall latency of the requests.diff --git a/litellm/constants.py b/litellm/constants.pyindex 180315ace0e..2bad2e212b6 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -213,6 +213,10 @@ REDIS_DAILY_AGENT_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_agent_spend_update_buREDIS_DAILY_TAG_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_tag_spend_update_buffer"MAX_REDIS_BUFFER_DEQUEUE_COUNT = int(os.getenv("MAX_REDIS_BUFFER_DEQUEUE_COUNT", 100))MAX_SIZE_IN_MEMORY_QUEUE = int(os.getenv("MAX_SIZE_IN_MEMORY_QUEUE", 2000))+# Bounds asyncio.Queue() instances (log queues, spend update queues, etc.) to prevent unbounded memory growth+LITELLM_ASYNCIO_QUEUE_MAXSIZE = int(+ os.getenv("LITELLM_ASYNCIO_QUEUE_MAXSIZE", 1000)+)MAX_IN_MEMORY_QUEUE_FLUSH_COUNT = int(os.getenv("MAX_IN_MEMORY_QUEUE_FLUSH_COUNT", 1000))diff --git a/litellm/integrations/gcs_bucket/gcs_bucket.py b/litellm/integrations/gcs_bucket/gcs_bucket.pyindex 3cb62905531..0f1ba4a4093 100644--- a/litellm/integrations/gcs_bucket/gcs_bucket.py+++ b/litellm/integrations/gcs_bucket/gcs_bucket.py@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuplefrom urllib.parse import quotefrom litellm._logging import verbose_logger+from litellm.constants import LITELLM_ASYNCIO_QUEUE_MAXSIZEfrom litellm.integrations.additional_logging_utils import AdditionalLoggingUtilsfrom litellm.integrations.gcs_bucket.gcs_bucket_base import GCSBucketBasefrom litellm.proxy._types import CommonProxyErrors@@ -41,7 +42,9 @@ class GCSBucketLogger(GCSBucketBase, AdditionalLoggingUtils):batch_size=self.batch_size,flush_interval=self.flush_interval,)- self.log_queue: asyncio.Queue[GCSLogQueueItem] = asyncio.Queue() # type: ignore[assignment]+ self.log_queue: asyncio.Queue[GCSLogQueueItem] = asyncio.Queue( # type: ignore[assignment]+ maxsize=LITELLM_ASYNCIO_QUEUE_MAXSIZE+ )asyncio.create_task(self.periodic_flush())AdditionalLoggingUtils.__init__(self)@@ -69,6 +72,9 @@ class GCSBucketLogger(GCSBucketBase, AdditionalLoggingUtils):)if logging_payload is None:raise ValueError("standard_logging_object not found in kwargs")+ # When queue is at maxsize, flush immediately to make room (no blocking, no data dropped)+ if self.log_queue.full():+ await self.flush_queue()await self.log_queue.put(GCSLogQueueItem(payload=logging_payload, kwargs=kwargs, response_obj=response_obj@@ -91,9 +97,9 @@ class GCSBucketLogger(GCSBucketBase, AdditionalLoggingUtils):)if logging_payload is None:raise ValueError("standard_logging_object not found in kwargs")- # Add to logging queue - this will be flushed periodically- # Use asyncio.Queue.put() for thread-safe concurrent access- # If queue is full, this will block until space is available (backpressure)+ # When queue is at maxsize, flush immediately to make room (no blocking, no data dropped)+ if self.log_queue.full():+ await self.flush_queue()await self.log_queue.put(GCSLogQueueItem(payload=logging_payload, kwargs=kwargs, response_obj=response_objdiff --git a/litellm/proxy/db/db_transaction_queue/base_update_queue.py b/litellm/proxy/db/db_transaction_queue/base_update_queue.pyindex 202829b78b6..a5ec1c3eaf4 100644--- a/litellm/proxy/db/db_transaction_queue/base_update_queue.py+++ b/litellm/proxy/db/db_transaction_queue/base_update_queue.py@@ -10,14 +10,18 @@ from litellm._service_logger import ServiceLoggingservice_logger_obj = (ServiceLogging()) # used for tracking metrics for In memory buffer, redis buffer, pod lock manager-from litellm.constants import MAX_IN_MEMORY_QUEUE_FLUSH_COUNT, MAX_SIZE_IN_MEMORY_QUEUE+from litellm.constants import (+ LITELLM_ASYNCIO_QUEUE_MAXSIZE,+ MAX_IN_MEMORY_QUEUE_FLUSH_COUNT,+ MAX_SIZE_IN_MEMORY_QUEUE,+)class BaseUpdateQueue:"""Base class for in memory buffer for database transactions"""def __init__(self):- self.update_queue = asyncio.Queue()+ self.update_queue = asyncio.Queue(maxsize=LITELLM_ASYNCIO_QUEUE_MAXSIZE)self.MAX_SIZE_IN_MEMORY_QUEUE = MAX_SIZE_IN_MEMORY_QUEUEasync def add_update(self, update):diff --git a/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.py b/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.pyindex c3074e641b2..5ba8fb13596 100644--- a/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.py+++ b/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.py@@ -3,6 +3,7 @@ from copy import deepcopyfrom typing import Dict, List, Optionalfrom litellm._logging import verbose_proxy_logger+from litellm.constants import LITELLM_ASYNCIO_QUEUE_MAXSIZEfrom litellm.proxy._types import BaseDailySpendTransactionfrom litellm.proxy.db.db_transaction_queue.base_update_queue import (BaseUpdateQueue,@@ -54,7 +55,7 @@ class DailySpendUpdateQueue(BaseUpdateQueue):def __init__(self):super().__init__()self.update_queue: asyncio.Queue[Dict[str, BaseDailySpendTransaction]] = (- asyncio.Queue()+ asyncio.Queue(maxsize=LITELLM_ASYNCIO_QUEUE_MAXSIZE))async def add_update(self, update: Dict[str, BaseDailySpendTransaction]):diff --git a/litellm/proxy/db/db_transaction_queue/spend_update_queue.py b/litellm/proxy/db/db_transaction_queue/spend_update_queue.pyindex 9b0449bb9ab..c96564252d0 100644--- a/litellm/proxy/db/db_transaction_queue/spend_update_queue.py+++ b/litellm/proxy/db/db_transaction_queue/spend_update_queue.py@@ -2,6 +2,7 @@ import asynciofrom typing import Dict, List, Optionalfrom litellm._logging import verbose_proxy_logger+from litellm.constants import LITELLM_ASYNCIO_QUEUE_MAXSIZEfrom litellm.proxy._types import (DBSpendUpdateTransactions,Litellm_EntityType,@@ -21,7 +22,9 @@ class SpendUpdateQueue(BaseUpdateQueue):def __init__(self):super().__init__()- self.update_queue: asyncio.Queue[SpendUpdateQueueItem] = asyncio.Queue()+ self.update_queue: asyncio.Queue[SpendUpdateQueueItem] = asyncio.Queue(+ maxsize=LITELLM_ASYNCIO_QUEUE_MAXSIZE+ )async def flush_and_get_aggregated_db_spend_update_transactions(self,