Repository navigation
Conversation
…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]>
There was a problem hiding this comment.
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.
| 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 | ||
| ) |
There was a problem hiding this comment.
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.
| 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 | |
| ) |
There was a problem hiding this comment.
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]>
…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>
FIX #634
Starlette notices a client disconnect only after the backend's response headers arrive and the
StreamingResponsestarts. 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.disconnecttogether, the same pattern as vLLM's@with_cancellation:process_request'sfinallyreleases the in-flight slot. The router logs one INFO line (Client disconnected before headers ...) and returns 499 to the departed client.receive(), because a synthetic receive that replays the body would spin the event loop.Testing:
src/tests/test_client_disconnect.pyruns a real uvicorn router against a real aiohttp fake engine.-swhen doinggit commit[Bugfix],[Feat], and[CI].🤖 Generated with Claude Code
https://claude.ai/code/session_01EkWp1KKx8oCX8JA3mXdkVJ