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): warn when --max_requests_before_restart runs with a single worker (#46185)
Yassin Kortam581449b4d883
diff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.pyindex c7371f294f0..7764d877b98 100644--- a/litellm/proxy/proxy_cli.py+++ b/litellm/proxy/proxy_cli.py@@ -368,6 +368,26 @@ class ProxyInitializationHelpers:_apply_uvicorn_max_requests_jitter = apply_uvicorn_max_requests_jitter+ @staticmethod+ def single_worker_max_requests_warning(+ num_workers: int,+ max_requests_before_restart: int | None,+ run_gunicorn: bool,+ ) -> str | None:+ if max_requests_before_restart is None or num_workers > 1:+ return None+ outage: Final = (+ "gunicorn accepts no connections until the replacement worker has booted"+ if run_gunicorn+ else "uvicorn exits the process, so the container restarts"+ )+ return (+ f"LiteLLM Proxy: --max_requests_before_restart {max_requests_before_restart} with --num_workers "+ f"{num_workers} does not recycle a worker. After {max_requests_before_restart} requests, health "+ f"probes included, {outage}. Use --num_workers 2 or more with --max_requests_before_restart_jitter, "+ "or rely on pod replacement instead."+ )+@staticmethoddef _get_reload_options(config_path: str | None) -> dict:"""Build uvicorn reload kwargs so --reload also reacts to .env and YAML edits."""@@ -968,7 +988,12 @@ class ProxyInitializationHelpers:"--max_requests_before_restart",default=None,type=int,- help="Restart worker after this many requests (uvicorn: limit_max_requests, gunicorn: max_requests)",+ help=(+ "Recycle each worker after it has served this many HTTP requests, health probes included "+ "(uvicorn: limit_max_requests, gunicorn: max_requests). Only safe with --num_workers 2 or more. "+ "With a single worker the whole proxy stops serving when the limit is hit, uvicorn exits the "+ "process and gunicorn accepts no connections until the replacement worker has booted."+ ),envvar="MAX_REQUESTS_BEFORE_RESTART",)@click.option(@@ -1070,7 +1095,7 @@ def run_server(max_budget,test,local,- num_workers,+ num_workers: int,granian_threads,test_async,iam_token_db_auth,@@ -1092,7 +1117,7 @@ def run_server(validate_config: bool,keepalive_timeout,timeout_worker_healthcheck,- max_requests_before_restart,+ max_requests_before_restart: int | None,max_requests_before_restart_jitter: int | None,limit_concurrency: int | None,enforce_prisma_migration_check: bool,@@ -1571,6 +1596,14 @@ def run_server(# Optional: recycle uvicorn workers after N requestsif max_requests_before_restart is not None:uvicorn_args["limit_max_requests"] = max_requests_before_restart+ if run_hypercorn is False and run_granian is False:+ single_worker_warning: Final = ProxyInitializationHelpers.single_worker_max_requests_warning(+ num_workers=num_workers,+ max_requests_before_restart=max_requests_before_restart,+ run_gunicorn=run_gunicorn is True,+ )+ if single_worker_warning is not None:+ print(f"\033[1;33m{single_worker_warning}\033[0m\n")if run_gunicorn is False and run_hypercorn is False and run_granian is False:if limit_concurrency is not None:uvicorn_args["limit_concurrency"] = limit_concurrencydiff --git a/tests/unit/proxy/test_proxy_cli.py b/tests/unit/proxy/test_proxy_cli.pyindex c73b60f3c26..33f0216ff85 100644--- a/tests/unit/proxy/test_proxy_cli.py+++ b/tests/unit/proxy/test_proxy_cli.py@@ -1768,6 +1768,258 @@ class TestProxyInitializationHelpers:assert g_kwargs["max_requests_before_restart"] == 900assert g_kwargs["max_requests_before_restart_jitter"] == 75+ @pytest.mark.parametrize("run_gunicorn", [False, True])+ @pytest.mark.parametrize(+ "num_workers,max_requests_before_restart",+ [(1, None), (2, 5)],+ )+ def test_single_worker_max_requests_warning_returns_none(+ self, num_workers, max_requests_before_restart, run_gunicorn+ ):+ """no warning when the flag is unset or more than one worker serves"""+ warning = ProxyInitializationHelpers.single_worker_max_requests_warning(+ num_workers=num_workers,+ max_requests_before_restart=max_requests_before_restart,+ run_gunicorn=run_gunicorn,+ )+ assert warning is None++ def test_single_worker_max_requests_warning_uvicorn(self):+ warning = ProxyInitializationHelpers.single_worker_max_requests_warning(+ num_workers=1,+ max_requests_before_restart=5,+ run_gunicorn=False,+ )+ assert warning is not None+ assert "--max_requests_before_restart 5" in warning+ assert "--num_workers 1" in warning+ assert "uvicorn exits the process" in warning++ def test_single_worker_max_requests_warning_gunicorn(self):+ warning = ProxyInitializationHelpers.single_worker_max_requests_warning(+ num_workers=1,+ max_requests_before_restart=5,+ run_gunicorn=True,+ )+ assert warning is not None+ assert "gunicorn accepts no connections" in warning+ assert "uvicorn exits" not in warning++ @patch("uvicorn.run")+ @patch("builtins.print")+ @patch("litellm.proxy.db.prisma_client.PrismaManager.setup_database")+ def test_single_worker_max_requests_warning_printed_on_uvicorn(+ self, mock_setup_db, mock_print, mock_uvicorn_run+ ):+ """uvicorn single-worker startup prints the does-not-recycle warning"""+ from click.testing import CliRunner++ from litellm.proxy.proxy_cli import run_server++ runner = CliRunner()+ clean_env = {+ k: v+ for k, v in os.environ.items()+ if k not in ("DATABASE_URL", "DIRECT_URL")+ }+ with (+ patch.dict(os.environ, clean_env, clear=True),+ patch.dict(+ "sys.modules",+ {+ "proxy_server": MagicMock(+ app=MagicMock(),+ ProxyConfig=MagicMock(),+ KeyManagementSettings=MagicMock(),+ save_worker_config=MagicMock(),+ )+ },+ ),+ patch(+ "litellm.proxy.proxy_cli.ProxyInitializationHelpers.get_default_unvicorn_init_args"+ ) as mock_get_args,+ ):+ mock_get_args.return_value = {+ "app": "litellm.proxy.proxy_server:app",+ "host": "localhost",+ "port": 8000,+ }++ result = runner.invoke(+ run_server, ["--local", "--max_requests_before_restart", "123"]+ )++ assert (+ result.exit_code == 0+ ), f"exit_code={result.exit_code}, output={result.output}"+ assert any(+ "--max_requests_before_restart 123 with --num_workers 1" in str(c)+ and "uvicorn exits the process" in str(c)+ for c in mock_print.call_args_list+ )++ @patch("litellm.proxy.proxy_cli.start_query_engine_reaper")+ @patch("uvicorn.run")+ @patch("builtins.print")+ @patch("litellm.proxy.db.prisma_client.PrismaManager.setup_database")+ def test_single_worker_max_requests_no_warning_with_two_workers(+ self, mock_setup_db, mock_print, mock_uvicorn_run, mock_start_reaper+ ):+ """num_workers 2 with --max_requests_before_restart prints no warning"""+ from click.testing import CliRunner++ from litellm.proxy.proxy_cli import run_server++ runner = CliRunner()+ clean_env = {+ k: v+ for k, v in os.environ.items()+ if k not in ("DATABASE_URL", "DIRECT_URL")+ }+ with (+ patch.dict(os.environ, clean_env, clear=True),+ patch.dict(+ "sys.modules",+ {+ "proxy_server": MagicMock(+ app=MagicMock(),+ ProxyConfig=MagicMock(),+ KeyManagementSettings=MagicMock(),+ save_worker_config=MagicMock(),+ )+ },+ ),+ patch(+ "litellm.proxy.proxy_cli.ProxyInitializationHelpers.get_default_unvicorn_init_args"+ ) as mock_get_args,+ ):+ mock_get_args.return_value = {+ "app": "litellm.proxy.proxy_server:app",+ "host": "localhost",+ "port": 8000,+ }++ result = runner.invoke(+ run_server,+ ["--local", "--num_workers", "2", "--max_requests_before_restart", "123"],+ )++ assert (+ result.exit_code == 0+ ), f"exit_code={result.exit_code}, output={result.output}"+ assert not any(+ "does not recycle a worker" in str(c) for c in mock_print.call_args_list+ )++ @patch("litellm.proxy.proxy_cli.ProxyInitializationHelpers.run_gunicorn_server")+ @patch("uvicorn.run")+ @patch("builtins.print")+ @patch("litellm.proxy.db.prisma_client.PrismaManager.setup_database")+ def test_single_worker_max_requests_warning_printed_on_gunicorn(+ self, mock_setup_db, mock_print, mock_uvicorn_run, mock_run_gunicorn+ ):+ """--run_gunicorn single-worker startup prints the gunicorn warning variant"""+ from click.testing import CliRunner++ from litellm.proxy.proxy_cli import run_server++ runner = CliRunner()+ clean_env = {+ k: v+ for k, v in os.environ.items()+ if k not in ("DATABASE_URL", "DIRECT_URL")+ }+ with (+ patch.dict(os.environ, clean_env, clear=True),+ patch.dict(+ "sys.modules",+ {+ "proxy_server": MagicMock(+ app=MagicMock(),+ ProxyConfig=MagicMock(),+ KeyManagementSettings=MagicMock(),+ save_worker_config=MagicMock(),+ )+ },+ ),+ patch(+ "litellm.proxy.proxy_cli.ProxyInitializationHelpers.get_default_unvicorn_init_args"+ ) as mock_get_args,+ ):+ mock_get_args.return_value = {+ "app": "litellm.proxy.proxy_server:app",+ "host": "localhost",+ "port": 8000,+ }++ result = runner.invoke(+ run_server,+ ["--local", "--run_gunicorn", "--max_requests_before_restart", "123"],+ )++ assert (+ result.exit_code == 0+ ), f"exit_code={result.exit_code}, output={result.output}"+ assert any(+ "--max_requests_before_restart 123 with --num_workers 1" in str(c)+ and "gunicorn accepts no connections" in str(c)+ for c in mock_print.call_args_list+ )++ @patch("litellm.proxy.proxy_cli.ProxyInitializationHelpers.init_hypercorn_server")+ @patch("uvicorn.run")+ @patch("builtins.print")+ @patch("litellm.proxy.db.prisma_client.PrismaManager.setup_database")+ def test_single_worker_max_requests_no_warning_on_hypercorn(+ self, mock_setup_db, mock_print, mock_uvicorn_run, mock_init_hypercorn+ ):+ """hypercorn ignores --max_requests_before_restart, so no warning prints"""+ from click.testing import CliRunner++ from litellm.proxy.proxy_cli import run_server++ runner = CliRunner()+ clean_env = {+ k: v+ for k, v in os.environ.items()+ if k not in ("DATABASE_URL", "DIRECT_URL")+ }+ with (+ patch.dict(os.environ, clean_env, clear=True),+ patch.dict(+ "sys.modules",+ {+ "proxy_server": MagicMock(+ app=MagicMock(),+ ProxyConfig=MagicMock(),+ KeyManagementSettings=MagicMock(),+ save_worker_config=MagicMock(),+ )+ },+ ),+ patch(+ "litellm.proxy.proxy_cli.ProxyInitializationHelpers.get_default_unvicorn_init_args"+ ) as mock_get_args,+ ):+ mock_get_args.return_value = {+ "app": "litellm.proxy.proxy_server:app",+ "host": "localhost",+ "port": 8000,+ }++ result = runner.invoke(+ run_server,+ ["--local", "--run_hypercorn", "--max_requests_before_restart", "5"],+ )++ assert (+ result.exit_code == 0+ ), f"exit_code={result.exit_code}, output={result.output}"+ mock_init_hypercorn.assert_called_once()+ assert not any(+ "does not recycle a worker" in str(c) for c in mock_print.call_args_list+ )+@pytest.mark.skipif(os.name == "nt", reason="gunicorn server path skips Windows")def test_gunicorn_options_include_max_requests_jitter(self):"""_run_gunicorn_server puts max_requests_jitter into the gunicorn options"""