launcher.py 3.8 KB
Newer Older
1
2
import asyncio
import signal
3
from http import HTTPStatus
4
from typing import Any, Optional
5
6

import uvicorn
7
from fastapi import FastAPI, Request, Response
8

9
10
from vllm import envs
from vllm.engine.async_llm_engine import AsyncEngineDeadError
11
from vllm.logger import init_logger
12
from vllm.utils import find_process_using_port
13
14
15
16

logger = init_logger(__name__)


17
async def serve_http(app: FastAPI, limit_concurrency: Optional[int],
18
                     **uvicorn_kwargs: Any):
19
20
21
22
23
24
25
26
27
28
    logger.info("Available routes are:")
    for route in app.routes:
        methods = getattr(route, "methods", None)
        path = getattr(route, "path", None)

        if methods is None or path is None:
            continue

        logger.info("Route: %s, Methods: %s", path, ', '.join(methods))

29
30
    # Set concurrency limits in uvicorn if running in multiprocessing mode
    # since zmq has maximum socket limit of zmq.constants.SOCKET_LIMIT (65536).
31
    if limit_concurrency is not None:
32
33
34
        logger.info(
            "Launching Uvicorn with --limit_concurrency %s. To avoid this "
            "limit at the expense of performance run with "
35
36
            "--disable-frontend-multiprocessing", limit_concurrency)
        uvicorn_kwargs["limit_concurrency"] = limit_concurrency
37

38
39
    config = uvicorn.Config(app, **uvicorn_kwargs)
    server = uvicorn.Server(config)
40
    _add_shutdown_handlers(app, server)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

    loop = asyncio.get_running_loop()

    server_task = loop.create_task(server.serve())

    def signal_handler() -> None:
        # prevents the uvicorn signal handler to exit early
        server_task.cancel()

    async def dummy_shutdown() -> None:
        pass

    loop.add_signal_handler(signal.SIGINT, signal_handler)
    loop.add_signal_handler(signal.SIGTERM, signal_handler)

    try:
        await server_task
        return dummy_shutdown()
    except asyncio.CancelledError:
60
61
62
63
64
65
        port = uvicorn_kwargs["port"]
        process = find_process_using_port(port)
        if process is not None:
            logger.debug(
                "port %s is used by process %s launched with command:\n%s",
                port, process, " ".join(process.cmdline()))
66
67
        logger.info("Gracefully stopping http server")
        return server.shutdown()
68
69


70
def _add_shutdown_handlers(app: FastAPI, server: uvicorn.Server) -> None:
71
72
73
    """Adds handlers for fatal errors that should crash the server"""

    @app.exception_handler(RuntimeError)
74
    async def runtime_error_handler(request: Request, __):
75
76
77
        """On generic runtime error, check to see if the engine has died.
        It probably has, in which case the server will no longer be able to
        handle requests. Trigger a graceful shutdown with a SIGTERM."""
78
        engine = request.app.state.engine_client
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        if (not envs.VLLM_KEEP_ALIVE_ON_ENGINE_DEATH and engine.errored
                and not engine.is_running):
            logger.fatal("AsyncLLMEngine has failed, terminating server "
                         "process")
            # See discussions here on shutting down a uvicorn server
            # https://github.com/encode/uvicorn/discussions/1103
            # In this case we cannot await the server shutdown here because
            # this handler must first return to close the connection for
            # this request.
            server.should_exit = True

        return Response(status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

    @app.exception_handler(AsyncEngineDeadError)
    async def engine_dead_handler(_, __):
        """Kill the server if the async engine is already dead. It will
        not handle any further requests."""
        if not envs.VLLM_KEEP_ALIVE_ON_ENGINE_DEATH:
            logger.fatal("AsyncLLMEngine is already dead, terminating server "
                         "process")
            server.should_exit = True

        return Response(status_code=HTTPStatus.INTERNAL_SERVER_ERROR)