Repository navigation
[serve][llm] Surface engine errors on the direct-streaming ASGI app - #65440
Conversation
Signed-off-by: Jeffrey Wang <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request resolves an issue where vLLM's error handler raises an AttributeError when attempting to read app.state.server by initializing it as a types.SimpleNamespace(). It also adds a corresponding unit test to verify that engine errors are correctly propagated to the client. The review feedback suggests initializing the namespace with should_exit=False to be more robust against future vLLM changes, and importing EngineDeadError lazily inside the test method rather than at the module level to prevent import failures in environments where the experimental v1 package is unavailable.
| # observe that flag (Serve restarts the replica via check_health), but the | ||
| # read must still succeed: otherwise it raises AttributeError, and that | ||
| # replaces the engine error in the response. | ||
| app.state.server = types.SimpleNamespace() |
There was a problem hiding this comment.
To be more defensive and robust against future changes in vLLM, it is safer to initialize the SimpleNamespace with should_exit=False. If a future version of vLLM or any other middleware attempts to read app.state.server.should_exit before writing to it, this will prevent an AttributeError.
| app.state.server = types.SimpleNamespace() | |
| app.state.server = types.SimpleNamespace(should_exit=False) |
| from fastapi.testclient import TestClient | ||
| from vllm.entrypoints.openai.cli_args import make_arg_parser | ||
| from vllm.utils.argparse_utils import FlexibleArgumentParser | ||
| from vllm.v1.engine.exceptions import EngineDeadError |
There was a problem hiding this comment.
Importing EngineDeadError from vllm.v1.engine.exceptions at the module level can cause the entire test module to fail to load if the installed vLLM version does not have the experimental v1 package or if its structure changes. To make the test suite more robust, we should avoid importing from vllm.v1 at the module level and instead import it lazily inside the test case.
| from fastapi.testclient import TestClient | |
| from vllm.entrypoints.openai.cli_args import make_arg_parser | |
| from vllm.utils.argparse_utils import FlexibleArgumentParser | |
| from vllm.v1.engine.exceptions import EngineDeadError | |
| from fastapi.testclient import TestClient | |
| from vllm.entrypoints.openai.cli_args import make_arg_parser | |
| from vllm.utils.argparse_utils import FlexibleArgumentParser |
| async def test_engine_error_reaches_the_client(self): | ||
| """A dead engine reports vLLM's error, not the handler's own AttributeError. | ||
|
|
||
| vLLM's engine error handler reads ``app.state.server``, which neither | ||
| ``build_app`` nor ``init_app_state`` sets. | ||
| """ |
There was a problem hiding this comment.
Import EngineDeadError lazily here to prevent module-level import failures if vllm.v1 is not available or structured differently in the environment.
| async def test_engine_error_reaches_the_client(self): | |
| """A dead engine reports vLLM's error, not the handler's own AttributeError. | |
| vLLM's engine error handler reads ``app.state.server``, which neither | |
| ``build_app`` nor ``init_app_state`` sets. | |
| """ | |
| async def test_engine_error_reaches_the_client(self): | |
| """A dead engine reports vLLM's error, not the handler's own AttributeError. | |
| vLLM's engine error handler reads ``app.state.server``, which neither | |
| ``build_app`` nor ``init_app_state`` sets. | |
| """ | |
| from vllm.v1.engine.exceptions import EngineDeadError |
Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: Jeffrey Wang <[email protected]>
…ay-project#65440) ## Description vLLM's **engine error handler** reads `app.state.server` to flip `should_exit` and stop the uvicorn server its launcher created. `VLLMEngine.build_asgi_app` calls only `build_app` and `init_app_state`, neither of which sets it. So on the direct-streaming path, every `EngineGenerateError` / `EngineDeadError` raises `AttributeError` inside the handler, and that replaces the engine error, and therefore clients get 'State' object has no attribute 'server' instead of the real failure. ## Solution Supply a dummy object that just tolerates an attribute write `types.SimpleNamespace()`. Nothing reads `should_exit`; `terminate_if_errored` only assigns it. With the read satisfied, the handler runs on to `create_error_response(exc)` and returns vLLM's real error. Discarding the write is safe: `should_exit` only stops vLLM's uvicorn loop, which Ray never runs — Serve restarts replicas via `LLMServer.check_health`. ## Reproduction Stand up a server with the following script and environment variables `RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1` and `RAY_SERVE_ENABLE_HA_PROXY=1`. ``` # serve_app.py from ray import serve from ray.serve.llm import LLMConfig, LLMServingArgs, build_openai_app llm_config = LLMConfig( model_loading_config=dict( model_id="qwen", model_source="Qwen/Qwen3-0.6B", ), deployment_config=dict( autoscaling_config=dict(min_replicas=1, max_replicas=1), ), ) serve.run(build_openai_app(LLMServingArgs(llm_configs=[llm_config])), blocking=True) ``` - Before ``` { "error": { "message": "'State' object has no attribute 'server'", "type": "InternalServerError", "param": null, "code": 500 } } ``` - After ``` { "error": { "message": "EngineCore encountered an issue. See stack trace (above) for the root cause.", "type": "InternalServerError", "param": null, "code": 500 } } ``` ## Related issues > Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to ray-project#1234". ## Additional information ### Why is this bug only discovered when we upgrade to vLLM 0.27.0? `app.state.server` was always missing in Ray: only vLLM's launcher sets it, and Ray runs no uvicorn loop. In 0.26, handlers were registered per exception type: a bad request stayed a `ValueError` and matched a handler that never reads `state.server`. Only a genuine engine death reached the one that does, which release tests never provoked. In 0.27, `generate()` wraps bad requests as `EngineGenerateError`, routing routine 4xx traffic through that handler and exposing the `AttributeError`. --------- Signed-off-by: Jeffrey Wang <[email protected]> Co-authored-by: Claude Opus 5 <[email protected]> Signed-off-by: 400Ping <[email protected]>
…ay-project#65440) ## Description vLLM's **engine error handler** reads `app.state.server` to flip `should_exit` and stop the uvicorn server its launcher created. `VLLMEngine.build_asgi_app` calls only `build_app` and `init_app_state`, neither of which sets it. So on the direct-streaming path, every `EngineGenerateError` / `EngineDeadError` raises `AttributeError` inside the handler, and that replaces the engine error, and therefore clients get 'State' object has no attribute 'server' instead of the real failure. ## Solution Supply a dummy object that just tolerates an attribute write `types.SimpleNamespace()`. Nothing reads `should_exit`; `terminate_if_errored` only assigns it. With the read satisfied, the handler runs on to `create_error_response(exc)` and returns vLLM's real error. Discarding the write is safe: `should_exit` only stops vLLM's uvicorn loop, which Ray never runs — Serve restarts replicas via `LLMServer.check_health`. ## Reproduction Stand up a server with the following script and environment variables `RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1` and `RAY_SERVE_ENABLE_HA_PROXY=1`. ``` # serve_app.py from ray import serve from ray.serve.llm import LLMConfig, LLMServingArgs, build_openai_app llm_config = LLMConfig( model_loading_config=dict( model_id="qwen", model_source="Qwen/Qwen3-0.6B", ), deployment_config=dict( autoscaling_config=dict(min_replicas=1, max_replicas=1), ), ) serve.run(build_openai_app(LLMServingArgs(llm_configs=[llm_config])), blocking=True) ``` - Before ``` { "error": { "message": "'State' object has no attribute 'server'", "type": "InternalServerError", "param": null, "code": 500 } } ``` - After ``` { "error": { "message": "EngineCore encountered an issue. See stack trace (above) for the root cause.", "type": "InternalServerError", "param": null, "code": 500 } } ``` ## Related issues > Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to ray-project#1234". ## Additional information ### Why is this bug only discovered when we upgrade to vLLM 0.27.0? `app.state.server` was always missing in Ray: only vLLM's launcher sets it, and Ray runs no uvicorn loop. In 0.26, handlers were registered per exception type: a bad request stayed a `ValueError` and matched a handler that never reads `state.server`. Only a genuine engine death reached the one that does, which release tests never provoked. In 0.27, `generate()` wraps bad requests as `EngineGenerateError`, routing routine 4xx traffic through that handler and exposing the `AttributeError`. --------- Signed-off-by: Jeffrey Wang <[email protected]> Co-authored-by: Claude Opus 5 <[email protected]>
Description
vLLM's engine error handler reads
app.state.serverto flipshould_exitand stop the uvicorn server its launcher created.VLLMEngine.build_asgi_appcalls onlybuild_appandinit_app_state, neither of which sets it. So on the direct-streaming path, everyEngineGenerateError/EngineDeadErrorraisesAttributeErrorinside the handler, and that replaces the engine error, and therefore clients get 'State' object has no attribute 'server' instead of the real failure.Solution
Supply a dummy object that just tolerates an attribute write
types.SimpleNamespace(). Nothing readsshould_exit;terminate_if_erroredonly assigns it. With the read satisfied, the handler runs on tocreate_error_response(exc)and returns vLLM's real error. Discarding the write is safe:should_exitonly stops vLLM's uvicorn loop, which Ray never runs — Serve restarts replicas viaLLMServer.check_health.Reproduction
Stand up a server with the following script and environment variables
RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1andRAY_SERVE_ENABLE_HA_PROXY=1.Related issues
Additional information
Why is this bug only discovered when we upgrade to vLLM 0.27.0?
app.state.serverwas always missing in Ray: only vLLM's launcher sets it, and Ray runs no uvicorn loop. In 0.26, handlers were registered per exception type: a bad request stayed aValueErrorand matched a handler that never readsstate.server. Only a genuine engine death reached the one that does, which release tests never provoked. In 0.27,generate()wraps bad requests asEngineGenerateError, routing routine 4xx traffic through that handler and exposing theAttributeError.