Skip to content

[Bugfix][Router] Abort the backend request when the client disconnects before headers - #1233

Closed
Mazyod wants to merge 2 commits into
vllm-project:mainfrom
Mazyod:fix/abort-on-early-client-disconnect
Closed

Mazyod wants to merge 2 commits into
vllm-project:mainfrom
Mazyod:fix/abort-on-early-client-disconnect

Conversation

@Mazyod

@Mazyod Mazyod commented Nov 24, 2026

Copy link
Copy Markdown

FIX #634

Starlette notices a client disconnect only after the backend's response headers arrive and the StreamingResponse starts. vLLM sends a non-streaming response's headers only once generation finishes. So until then nothing watches the client. A non-streaming request whose client cancels keeps generating on the engine to the end, because the proxy session has no read timeout. Streams are already handled after headers.

This PR waits for the backend headers and the client's http.disconnect together, the same pattern as vLLM's @with_cancellation:

  • If the client leaves first, the backend attempt is cancelled. aiohttp closes the upstream connection, vLLM aborts the request, and process_request's finally releases the in-flight slot. The router logs one INFO line (Client disconnected before headers ...) and returns 499 to the departed client.
  • If the headers arrive first, nothing changes.
  • The watcher reads a single ASGI message after the body. If that message isn't a disconnect, or reading fails, it stops watching and the request proceeds exactly as today. It never loops on receive(), because a synthetic receive that replays the body would spin the event loop.

Testing:

  • New src/tests/test_client_disconnect.py runs a real uvicorn router against a real aiohttp fake engine.
    • A non-streaming client disconnects at 0.5 s while the engine holds its headers for 20 s. The engine must see the upstream connection close within 1 s. This fails on main, where the engine holds the request for the full time, and passes with this change.
    • A normal non-streaming completion and a stream disconnect after headers are unchanged.
  • All 286 router tests pass (Python 3.12, freshly resolved dependencies: Starlette 1.10.0, uvicorn 0.34.0, aiohttp 3.14.5). Pre-commit passes.
  • We have run the same change in our production fork (Mazyod/production-stack), where clients that timed out after about 8 s left non-streaming requests queued on a vLLM backend. In a live check, the backend saw the connection close 2.0 s after a client left at 2 s; before the fix it kept the request until generation finished.

  • Make sure the code changes pass the pre-commit checks.
  • Sign-off your commit by using -s when doing git commit
  • Try to classify PRs for easy understanding of the type of changes, such as [Bugfix], [Feat], and [CI].

🤖 Generated with Claude Code

https://claude.ai/code/session_01EkWp1KKx8oCX8JA3mXdkVJ

…s before headers

Starlette notices a client disconnect only once the StreamingResponse
starts, which is after the backend's response headers arrive. vLLM sends a
non-streaming response's headers only when generation finishes. So a client
that cancels a non-streaming request leaves the engine generating until the
end, since the proxy session has no read timeout (vllm-project#634).

route_general_request now waits for the backend headers and the client's
http.disconnect together, like vLLM's @with_cancellation. If the client
leaves first, the router cancels the backend attempt: the aiohttp request
closes its connection, so vLLM aborts the request, and process_request's
finally releases the in-flight slot. The router logs one INFO line and
returns 499 to the departed client. Disconnects after headers, including
streams, keep their current handling.

The watcher reads a single ASGI message after the body. If that message is
not a disconnect, or reading fails, it stops watching and the request
proceeds as before. It never loops on receive(), because a receive that
replays the body would spin the event loop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EkWp1KKx8oCX8JA3mXdkVJ
Signed-off-by: Mazyad Alabduljaleel <[email protected]>

@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 introduces a mechanism to detect client disconnects before headers are received from the backend engine, allowing the router to abort the upstream request and return a 499 status code. This is supported by a new integration test suite. The review feedback suggests modernizing the asynchronous task creation in _headers_unless_client_disconnects by using asyncio.create_task instead of asyncio.ensure_future and passing a set of tasks to asyncio.wait.

Comment on lines +254 to +259
headers_task = asyncio.ensure_future(anext(stream_generator))
disconnect_task = asyncio.ensure_future(_client_disconnected(request))
try:
await asyncio.wait(
(headers_task, disconnect_task), return_when=asyncio.FIRST_COMPLETED
)

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

Using asyncio.create_task is the modern and preferred way to spawn tasks in Python 3.7+ compared to asyncio.ensure_future. Additionally, passing a set of tasks to asyncio.wait is more idiomatic and standard than passing a tuple.

Suggested change
headers_task = asyncio.ensure_future(anext(stream_generator))
disconnect_task = asyncio.ensure_future(_client_disconnected(request))
try:
await asyncio.wait(
(headers_task, disconnect_task), return_when=asyncio.FIRST_COMPLETED
)
headers_task = asyncio.create_task(anext(stream_generator))
disconnect_task = asyncio.create_task(_client_disconnected(request))
try:
await asyncio.wait(
{headers_task, disconnect_task}, return_when=asyncio.FIRST_COMPLETED
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Applied in 7ebfca8: both waits now use asyncio.create_task, and asyncio.wait gets sets. No behavior change; all 286 router tests pass.

Address review feedback: use asyncio.create_task and pass sets to asyncio.wait. Behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EkWp1KKx8oCX8JA3mXdkVJ
Signed-off-by: Mazyad Alabduljaleel <[email protected]>
@Mazyod

Mazyod commented Nov 24, 2026

Copy link
Copy Markdown
Author

Closing in favour of #1040, which takes the same approach, came first, and handles more edge cases (caller cancellation and log sanitizing). I verified #1040 against current main and posted the results there. Our end-to-end test stays on this branch in case it's useful.

@Mazyod Mazyod closed this Nov 24, 2026
Mazyod pushed a commit to Mazyod/production-stack that referenced this pull request Nov 24, 2026
…instead of vllm-project#1233

PR vllm-project#1040 already proposed the same early-disconnect fix for vllm-project#634, and it
covers more edge cases. It passed upstream's suite and our real-socket test
once rebased, so we closed our duplicate vllm-project#1233 and posted that evidence on
vllm-project#1040. Record that, and the lesson: read the issue's timeline for linked PRs
before opening a PR, since keyword search missed vllm-project#1040. Docs-only.

Signed-off-by: ci-sim <ci@sim>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EkWp1KKx8oCX8JA3mXdkVJ
Signed-off-by: ci-sim <ci@sim>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Cancellations don't propagate through the vLLM router to the engines

1 participant