mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
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>
This commit is contained in:
parent
7e0728cb29
commit
de2a0357d5
2 changed files with 32 additions and 4 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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"),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue