Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/litserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def __init__(
api_path: str = "/predict",
healthcheck_path: str = "/health",
info_path: str = "/info",
shutdown_path: str = "/shutdown",
model_metadata: Optional[dict] = None,
stream: bool = False,
spec: Optional[LitSpec] = None,
Expand All @@ -154,6 +155,7 @@ def __init__(
api_path: URL path for the prediction endpoint.
healthcheck_path: URL path for the health check endpoint.
info_path: URL path for the server and model information endpoint.
shutdown_path: URL path for the server shutdown endpoint.
model_metadata: Metadata about the model, shown at the info endpoint.
stream: Whether to enable streaming responses.
spec: Specification for the API, such as OpenAISpec or custom specs.
Expand Down Expand Up @@ -220,6 +222,9 @@ def __init__(
"info_path must start with '/'. Please provide a valid api path like '/info', '/details', or '/v1/info'"
)

if not shutdown_path.startswith("/"):
raise ValueError("shutdown_path must start with '/'. Please provide a valid api path like '/shutdown'")

try:
json.dumps(model_metadata)
except (TypeError, ValueError):
Expand All @@ -243,6 +248,7 @@ def __init__(
self.api_path = api_path
self.healthcheck_path = healthcheck_path
self.info_path = info_path
self.shutdown_path = shutdown_path
self.track_requests = track_requests
self.timeout = timeout
lit_api.stream = stream
Expand Down Expand Up @@ -447,6 +453,14 @@ async def info(request: Request) -> Response:
}
)

@self.app.post(self.shutdown_path, dependencies=[Depends(self.setup_auth())])
async def shutdown(request: Request):
server = self.app.state.server
print("Initiating shutdown...")
server.should_exit = True

return Response(content="Server has been shutdown")

async def predict(request: self.request_type) -> self.response_type:
self._callback_runner.trigger_event(
EventTypes.ON_REQUEST.value,
Expand Down Expand Up @@ -651,6 +665,7 @@ def _start_server(self, port, num_uvicorn_servers, log_level, sockets, uvicorn_w
# https://github.com/encode/uvicorn/pull/802
config.workers = num_uvicorn_servers
server = uvicorn.Server(config=config)
self.app.state.server = server
if uvicorn_worker_type == "process":
ctx = mp.get_context("fork")
w = ctx.Process(target=server.run, args=(sockets,))
Expand Down
21 changes: 21 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import ExitStack
from types import SimpleNamespace

import numpy as np
import pytest
Expand Down Expand Up @@ -147,6 +148,26 @@ def test_workers_health_with_custom_health_method(use_zmq):
assert response.text == "ok"


def test_shutdown_endpoint():
server = LitServer(
SlowSetupLitAPI(),
accelerator="cpu",
shutdown_path="/shutdown",
devices=1,
workers_per_device=1,
)

server.app.state.server = SimpleNamespace(should_exit=False)

with wrap_litserve_start(server) as server, TestClient(server.app) as client:
# Send a shutdown request
response = client.post("/shutdown")
assert response.status_code == 200
assert "shutdown" in response.text.lower()
time.sleep(0.5)
assert server.app.state.server.should_exit is True, "Server should be marked for shutdown"


def make_load_request(server, outputs):
with TestClient(server.app) as client:
for i in range(100):
Expand Down