Skip to content

[Router][Bugfix] propagate client cancellation - #1040

Open
unsorted-otter wants to merge 8 commits into
vllm-project:mainfrom
unsorted-otter:bugfix/propagate-client-cancellation
Open

unsorted-otter wants to merge 8 commits into
vllm-project:mainfrom
unsorted-otter:bugfix/propagate-client-cancellation

Conversation

@unsorted-otter

@unsorted-otter unsorted-otter commented Aug 19, 2026 •

Copy link
Copy Markdown

When a client disconnects from a non-streaming request, the router just keeps waiting for the backend's response headers, it never checks whether the client is still around. So the engine has no way to know it should stop, runs the request to completion, and the router throws the answer away.

This races waiting for the backend headers against the client disconnecting, same idea as vLLM's own entrypoints.utils.with_cancellation. Whichever happens first wins: if the client leaves, the pending call gets cancelled, which closes the backend connection and lets the engine abort. A normal response works exactly as before.

#867 already tried to fix this and never got merged. Two differences here, both directly answering what that review pointed out:

  • It polled request.is_disconnected(). vLLM's own code avoids that on purpose, because it breaks once there's middleware in the stack. This uses a background task racing request.receive() for http.disconnect instead, same as with_cancellation.
  • Its review caught a log-injection issue from logging the raw X-Request-Id header. The one new log line here strips that before logging it.

Test plan

  • src/tests/test_client_disconnect.py: covers a client disconnecting before the backend responds (checks the backend call actually gets torn down, not just that we return 499), a normal connected client still getting its response, and the cleanup helper correctly telling apart its own cancellation from an unrelated one hitting the caller at the same time (there's a test that fails against the naive version of that check, so it doesn't creep back in).
  • Ran pytest src/tests/test_client_disconnect.py locally, all 6 pass.
  • Merged current main (405a2fa): the full router suite passes on Python 3.12 (289 tests)
  • black, isort, and ruff all pass on the changed files.

Fixes #634


  • 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].
Detailed Checklist (Click to Expand)

Thank you for your contribution to production-stack! Before submitting the pull request, please ensure the PR meets the following criteria. This helps us maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Please try to classify PRs for easy understanding of the type of changes. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Feat] for new features in the cluster (e.g., autoscaling, disaggregated prefill, etc.).
  • [Router] for changes to the vllm_router (e.g., routing algorithm, router observability, etc.).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • Pass all linter checks. Please use pre-commit to format your code. See README.md for installation.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Please include sufficient tests to ensure the change is stay correct and robust. This includes both unit tests and integration tests.

DCO and Signed-off-by

When contributing changes to this project, you must agree to the DCO. Commits must include a Signed-off-by: header which certifies agreement with the terms of the DCO.

Using -s with git commit will automatically add this header.

What to Expect for the Reviews

We aim to address all PRs in a timely manner. If no one reviews your PR within 5 days, please @-mention one of YuhanLiu11
, Shaoting-Feng or ApostaC.

The router waited for backend response headers before ever checking for
a client disconnect, so a non-streaming request kept running on the
engine after the client left.

Fixes vllm-project#634

Signed-off-by: unsorted-otter <[email protected]>
Shield the generator close, stop swallowing cancellation aimed at our
own task, and await the disconnect listener instead of only cancelling
it.

Signed-off-by: unsorted-otter <[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 implements client disconnect handling in the router, aborting backend requests and returning a 499 status code if a client disconnects before a response is sent. It also ensures request statistics are properly cleaned up on all exit paths. The review feedback highlights a potential resource leak in the disconnect handling logic, Python compatibility issues with asyncio.Task.cancelling() on Python versions older than 3.11, and a recommendation to make the log sanitization helper safer against non-string inputs.

Comment thread src/tests/test_client_disconnect.py Resolved
Comment thread src/tests/test_client_disconnect.py Resolved
unsorted-otter and others added 3 commits August 19, 2026 09:09
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: unsorted-otter <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: unsorted-otter <[email protected]>
@unsorted-otter unsorted-otter changed the title Bugfix/propagate client cancellation [Router][Bugfix] propagate client cancellation Aug 19, 2026
@Mazyod

Mazyod commented Nov 24, 2026

Copy link
Copy Markdown

Adding our support for this PR. We hit #634 in production: clients that timed out after about 8 s left their non-streaming requests running on a vLLM backend, and those requests helped fill its queue. We had written the same fix independently (#1233). I've closed that in favour of this PR, which came first and handles caller cancellation more carefully.

What we checked against current main (405a2fa):

  • Conflict: this branch conflicts only in process_request's finally. Since [Bugfix][Router] Fix leaked in-flight counters in RequestStatsMonitor #1072 and [Bugfix][Router] Key request stats by dispatch, not by X-Request-Id #1115, main already releases the in-flight slot there, keyed by stats_id, so keeping main's side resolves it. @unsorted-otter, that should be the whole rebase.
  • Tests: with that resolution, all 289 router tests pass on Python 3.12 (Starlette 1.10.0, uvicorn 0.34.0, aiohttp 3.14.5), including this PR's own tests.
  • End-to-end: we also ran our test against this branch. It puts a real uvicorn router in front of a real aiohttp engine.
    • A non-streaming client disconnects at 0.5 s while the engine holds its headers for 20 s. The engine sees the upstream connection close within 1 s, compared with the full 20 s on main.
    • A normal completion and a stream disconnect after headers are unchanged.
    • 3 of 3 runs passed.

The test is here if a real-socket check would usefully complement the mocked tests. We can send it as a follow-up PR once this merges.

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>
Resolve the conflict in process_request's finally block by keeping
main's side: vllm-project#1072 already moved on_request_complete into finally, and
vllm-project#1115 keys request stats by the per-dispatch stats_id instead of
request_id.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: unsorted-otter <[email protected]>
@unsorted-otter

unsorted-otter commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Thanks @Mazyod, really helpful. I've merged main and resolved the conflict exactly as you described.

@YuhanLiu11 @Shaoting-Feng @ApostaC this is up to date with main and ready for review, thanks.

This branch has not been deployed

No deployments
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

2 participants