Skip to content

[serve][llm] Surface engine errors on the direct-streaming ASGI app - #65440

Merged
eicherseiji merged 2 commits into
masterfrom
fix-asgi-engine-error-state
Aug 15, 2026
Merged

eicherseiji merged 2 commits into
masterfrom
fix-asgi-engine-error-state

Conversation

@jeffreywang88

@jeffreywang88 jeffreywang88 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

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 #1234", "Closes #1234", or "Related to #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.

@jeffreywang88
jeffreywang88 requested a review from a team as a code owner August 12, 2026 21:52
@jeffreywang88 jeffreywang88 added the go add ONLY when ready to merge, run all tests label Aug 12, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
app.state.server = types.SimpleNamespace()
app.state.server = types.SimpleNamespace(should_exit=False)

Comment on lines +10 to +13
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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

Comment on lines +797 to +802
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.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import EngineDeadError lazily here to prevent module-level import failures if vllm.v1 is not available or structured differently in the environment.

Suggested change
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

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue llm labels Aug 13, 2026

@eicherseiji eicherseiji left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eicherseiji
eicherseiji enabled auto-merge (squash) August 14, 2026 23:06
@eicherseiji
eicherseiji merged commit 026bf2b into master Aug 15, 2026
7 checks passed
@eicherseiji
eicherseiji deleted the fix-asgi-engine-error-state branch August 15, 2026 01:46
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…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]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests llm serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ray fails to serialize self-reference objects

2 participants