From de2a0357d50317237a5d8049bcde404dee506dab Mon Sep 17 00:00:00 2001 From: mateo Date: Sun, 1 Nov 2026 00:17:20 +0000 Subject: [PATCH] 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> --- litellm/proxy/dev_reload.py | 8 ++++---- tests/unit/proxy/test_dev_reload.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/dev_reload.py b/litellm/proxy/dev_reload.py index 43cb5364087..500f581b34d 100644 --- a/litellm/proxy/dev_reload.py +++ b/litellm/proxy/dev_reload.py @@ -65,11 +65,12 @@ def server_options(options: ReloadOptions) -> ServerOptions: ) -def load_app_or_exit(import_path: str) -> Callable[..., object]: +def load_app_or_exit(import_path: str, factory: bool = False) -> Callable[..., object]: module_name: Final = import_path.partition(":")[0] try: importlib.import_module(module_name) - return ASGI_APP.validate_python(import_path) + app: Final = ASGI_APP.validate_python(import_path) + return ASGI_APP.validate_python(app()) if factory else app except Exception: LOGGER.exception("Error loading ASGI app %s", import_path) sys.exit(uvicorn_config.STARTUP_FAILURE) @@ -85,8 +86,7 @@ def load_worker_app(import_path: str, worker_app_factory: bool) -> Callable[..., pool_parent: Final = multiprocessing.parent_process() if pool_parent is not None: threading.Thread(target=stop_worker_when_pool_exits, args=(pool_parent,), daemon=True).start() - worker_app: Final = load_app_or_exit(import_path) - return ASGI_APP.validate_python(worker_app()) if worker_app_factory else worker_app + return load_app_or_exit(import_path, worker_app_factory) def pool_config(options: ReloadOptions, workers: int, worker_overrides: Mapping[str, object] | None = None) -> Config: diff --git a/tests/unit/proxy/test_dev_reload.py b/tests/unit/proxy/test_dev_reload.py index 96ab8b26329..0eca5682402 100644 --- a/tests/unit/proxy/test_dev_reload.py +++ b/tests/unit/proxy/test_dev_reload.py @@ -56,6 +56,34 @@ def test_a_broken_app_exits_the_worker_as_a_startup_failure( assert exit_info.value.code == uvicorn_config.STARTUP_FAILURE +@REQUIRES_UVICORN_STARTUP_FAILURE +@pytest.mark.parametrize( + ("module", "source"), + ( + ( + "reload_factory_missing_module_app", + "def factory():\n import a_module_that_does_not_exist\n", + ), + ( + "reload_factory_runtime_error_app", + "def factory():\n raise RuntimeError('broken edit')\n", + ), + ), +) +def test_a_broken_factory_exits_the_worker_as_a_startup_failure( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, module: str, source: str +) -> None: + (tmp_path / f"{module}.py").write_text(source) + monkeypatch.syspath_prepend(str(tmp_path)) + with pytest.raises(SystemExit) as exit_info: + pool_config( + _options(f"{module}:app"), + workers=POOL_WORKERS, + worker_overrides={"app": f"{module}:factory", "factory": True}, + ).load() + assert exit_info.value.code == uvicorn_config.STARTUP_FAILURE + + @REQUIRES_UVICORN_STARTUP_FAILURE @pytest.mark.parametrize( ("module", "source"),