litellm/tests/integration/configuration/test_multi_worker_reload.py
devin-ai-integration[bot] 187f936cb3
fix(proxy)!: support reload with multiple workers (#45909)
* fix(proxy): support reload with multiple workers

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(proxy): stop the reload worker pool on a broken edit instead of respawning it

Workers now load the app through a factory that exits with uvicorn's
STARTUP_FAILURE code on any import error, so the pool stops and the
reloader waits for the next save, the same as single-worker reload.
Multi-worker reload needs uvicorn>=0.50.0 for that, so older versions
keep the single-worker reload with a warning.

* test(integration): stop the reload test's proxy through the harness shutdown helper

Main's TQ010 rule rejects a raw .terminate() under tests/integration, so the multi-worker reload test now calls OwnedProxy.begin_shutdown(), which sends the same SIGTERM

* test(proxy): cover the reload worker pool end to end and edit the config the proxy loads

* docs(agents): scope the two-worker reload QA rule to uvicorn proxies that use Redis

* fix(proxy): keep the original traceback when a reloaded worker fails to import the app

* docs(agents): point both router and coordination Redis at the QA Redis and cover single-worker trees

* fix(proxy): stop the reload worker pool when the reloader exits and give reloads a longer worker healthcheck

Closing the terminal or pressing Ctrl-\ killed the reloader and left the pool parent holding the port while it respawned workers that could not start. The pool now stops once its parent reloader is gone. The reload pool also defaults the worker healthcheck to 30 seconds unless --timeout_worker_healthcheck is set, since uvicorn's 5 second default killed workers that were still booting on a reload.

* fix(proxy): watch the reloader through its process sentinel so the pool also stops on Windows

* fix(proxy): carry the graceful shutdown timeout into the reload worker pool

* fix(proxy): stop the reload worker pool on Ctrl-\ instead of waiting out a dying worker's healthcheck

* test(proxy): skip the startup-failure reload tests on uvicorn without a startup failure code

* docs(agents): keep the QA proxy rule to two workers, Redis, and reload

* fix(proxy): keep Windows on single-worker reload and pass unlisted uvicorn args to the reload pool

* fix(proxy): stop reload workers when their pool parent dies

* test(proxy): move the reload pool process tests to the integration suite

The unit test guide keeps tests/unit in-process, so the subprocess pool cases now live in tests/integration/configuration and use the harness port allocator and process helpers

* docs(agents): keep main's proof rules and inline the multi-worker QA command

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(proxy): exit reload workers as startup failures when the app factory raises

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(proxy): check the reload pool serves the overridden worker app

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: mateo <mateo@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-01 00:57:32 +00:00

95 lines
3.8 KiB
Python

import json
import os
import re
from pathlib import Path
from typing import Final
import psutil
import pytest
from tests.integration._support.client import Gateway, eventually, object_value
from tests.integration._support.process import OwnedProxy, graceful_stop_seconds, owned_proxy_process
STARTED_WORKER: Final = re.compile(r"Started server process \[(\d+)\]")
def _worker_pids(owned: OwnedProxy) -> frozenset[int]:
return frozenset(int(match.group(1)) for match in STARTED_WORKER.finditer(owned.log.read_text()))
def _launched_config(owned: OwnedProxy) -> Path:
arguments: Final = psutil.Process(owned.process.pid).cmdline()
return Path(arguments[arguments.index("--config") + 1])
def _config(model: str) -> str:
return json.dumps(
{
"model_list": [{"model_name": model, "litellm_params": {"model": "openai/reload-test"}}],
"general_settings": {"master_key": "os.environ/LITELLM_MASTER_KEY"},
"router_settings": {"redis_host": "os.environ/REDIS_HOST", "redis_port": "os.environ/REDIS_PORT"},
}
)
def _assert_replaced(owned: OwnedProxy, previous: frozenset[int], workers: int) -> frozenset[int]:
started: Final = eventually(
lambda: _worker_pids(owned),
lambda pids: len(pids - previous) == workers,
seconds=graceful_stop_seconds(),
)
assert all(not psutil.pid_exists(pid) for pid in previous), owned.log.read_text()
assert owned.process.poll() is None, owned.log.read_text()
assert len(started) == len(previous) + workers, owned.log.read_text()
return started
def _assert_models(gateway: Gateway) -> None:
data: Final = gateway.get("/v1/models")["data"]
assert isinstance(data, list), data
assert tuple(object_value(model)["id"] for model in data) == ("after-reload",)
@pytest.mark.parametrize("workers", (1, 2))
@pytest.mark.timeout(6 * graceful_stop_seconds())
def test_reload_replaces_every_worker_and_shutdown_leaves_none(
gateway: Gateway, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, workers: int
) -> None:
root: Final = Path(__file__).resolve().parents[3]
monkeypatch.setenv("INTEGRATION_PROXY_ROOT", str(tmp_path))
config: Final = tmp_path / "config.yaml"
source: Final = tmp_path / "reload_source.py"
env: Final = tmp_path / ".env"
config.write_text(_config("before-reload"))
source.write_text("VALUE = 1\n")
env.write_text("RELOAD_TEST_VALUE=before\n")
with owned_proxy_process(
gateway,
tmp_path,
{
"PYTHONPATH": os.pathsep.join((str(root), str(root / "tests"), os.environ.get("PYTHONPATH", ""))),
"STORE_MODEL_IN_DB": "False",
},
config=config,
remove_environment=("DATABASE_URL", "DATABASE_URL_READ_REPLICA", "PYTHON_DOTENV_DISABLED"),
workers=workers,
database_setup=(),
extra_arguments=("--reload",),
) as owned:
initial: Final = eventually(
lambda: _worker_pids(owned), lambda pids: len(pids) == workers, seconds=graceful_stop_seconds()
)
_launched_config(owned).write_text(_config("after-reload"))
after_config: Final = _assert_replaced(owned, initial, workers)
for _ in range(2 * workers):
_assert_models(owned.gateway)
source.write_text("VALUE = 2\n")
after_source: Final = _assert_replaced(owned, after_config, workers)
env.write_text("RELOAD_TEST_VALUE=after\n")
after_env: Final = _assert_replaced(owned, after_source, workers)
response: Final = owned.gateway.request("GET", "/health/readiness")
assert response.status_code == 200, response.text
owned.begin_shutdown()
owned.process.wait(timeout=graceful_stop_seconds())
assert all(not psutil.pid_exists(pid) for pid in after_env), owned.log.read_text()
assert owned.process.returncode == 0, owned.log.read_text()