Skip to content

[Serve] Clean up unary proxy result task on timeout or disconnect - #65721

Closed
hsusul wants to merge 1 commit into
ray-project:masterfrom
hsusul:fix/serve-proxy-unary-task-cleanup
Closed

hsusul wants to merge 1 commit into
ray-project:masterfrom
hsusul:fix/serve-proxy-unary-task-cleanup

Conversation

@hsusul

@hsusul hsusul commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #65718.

When a unary Serve response timed out or the client disconnected, the proxy cancelled the underlying deployment response but left its local asyncio result task unmanaged. The streaming path already cancelled and drained its local task.

This change cancels and awaits the unary result task before propagating the timeout or cancellation. A focused regression test uses a pending awaitable and verifies that its local waiter has exited.

Validation

  • python -m py_compile python/ray/serve/_private/proxy_response_generator.py python/ray/serve/tests/test_proxy_response_generator.py
  • ruff check python/ray/serve/_private/proxy_response_generator.py python/ray/serve/tests/test_proxy_response_generator.py
  • git diff --check
  • Focused pytest is currently blocked in this checkout because the compiled ray._raylet module is not installed.

@hsusul
hsusul requested a review from a team as a code owner August 25, 2026 06:45

@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 ensures that the local result_task is properly cancelled and awaited upon client disconnection or timeout, and adds a corresponding unit test. The reviewer suggests a performance improvement: calling self._response.cancel() before awaiting the local result_task cancellation to immediately trigger the cancellation of the remote Ray task and free up cluster resources sooner.

Comment on lines +179 to 187
result_task.cancel()
await asyncio.gather(result_task, return_exceptions=True)
self._response.cancel()
raise asyncio.CancelledError()
else:
result_task.cancel()
await asyncio.gather(result_task, return_exceptions=True)
self._response.cancel()
raise TimeoutError()

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

Calling self._response.cancel() after awaiting the cancellation of result_task delays the cancellation of the underlying deployment response (and thus the remote Ray task). self._response.cancel() is responsible for cancelling the remote Ray task/actor call (via self._replica_result.cancel()). If we await result_task first, the remote Ray task continues to run on the cluster until the local result_task finishes its cancellation.

By calling self._response.cancel() first, we initiate the cancellation of the remote Ray task immediately, freeing up cluster resources as soon as possible, and then we safely cancel and await the local result_task.

Suggested change
result_task.cancel()
await asyncio.gather(result_task, return_exceptions=True)
self._response.cancel()
raise asyncio.CancelledError()
else:
result_task.cancel()
await asyncio.gather(result_task, return_exceptions=True)
self._response.cancel()
raise TimeoutError()
self._response.cancel()
result_task.cancel()
await asyncio.gather(result_task, return_exceptions=True)
raise asyncio.CancelledError()
else:
self._response.cancel()
result_task.cancel()
await asyncio.gather(result_task, return_exceptions=True)
raise TimeoutError()

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue community-contribution Contributed by the community labels Aug 25, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Sep 8, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no more activity in the 14 days
since being marked stale.

Please feel free to reopen or open a new pull request if you'd still like this to be addressed.

Again, you can always ask for help on our discussion forum or Ray's public slack channel.

Thanks again for your contribution!

@github-actions github-actions Bot closed this Sep 23, 2026
return wait_for_response().__await__()

response = Response()
gen = ProxyResponseGenerator(response, timeout_s=0.001)

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.

please rewrite the test without using ProxyResponseGenerator since its a private module

@johntaylor-cell johntaylor-cell removed the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Oct 14, 2026
@johntaylor-cell

Copy link
Copy Markdown
Contributor

Could you make the change requested by @abrarsheikh? Thanks

abrarsheikh pushed a commit that referenced this pull request Nov 5, 2026
…nnect (#67109)

## Description

On a timeout or client disconnect,
`ProxyResponseGenerator._get_unary_result()` cancels the
`DeploymentResponse` but never touches the local asyncio task that is
awaiting it. If cancelling the response doesn't promptly wake that
await, the proxy keeps a pending task (and the response it references)
until the response eventually resolves. The streaming path already
cancels its local task.

This change calls `self._response.cancel()` first, so the replica-side
cancellation starts right away. It then cancels and awaits the local
result task before raising `TimeoutError` / `CancelledError`.

This picks up #65721 by @hsusul, which was closed as stale, and
addresses its review feedback:
- **Tests don't use the private module:** the regression tests no longer
use `ProxyResponseGenerator` directly. Instead they go through the gRPC
proxy's unary entrypoint (timeout) and `HTTPProxy.__call__` (client
disconnect) in `tests/unit/test_proxy.py`. Both use a fake response
whose await is not woken by `cancel()`.
- **Cancel the response first:** it adopts the suggestion to call
`self._response.cancel()` before awaiting the local task.

With the current replica result implementations, `cancel()` wakes the
await promptly. I checked this end to end against gRPC timeouts,
including with a SIGSTOP'd replica. So this is defensive hardening
against delayed cancellation rather than a fix for a leak you can see in
today's code paths.

Tests:
- The two new tests fail without the fix and pass with it.
- Also passing: `tests/unit/test_proxy.py`,
`test_proxy_request_response.py`, `test_proxy_router.py`,
`tests/test_proxy_response_generator.py`,
`tests/test_request_timeout.py` and `tests/test_grpc.py`.

## Related issues

Closes #65718

## Additional information

The streaming path still uses the cancel-plus-done-callback style. I
kept that unchanged to keep this PR focused, and I'm happy to align the
two if you'd prefer.

cc @abrarsheikh

**Why this isn't a duplicate:** the only earlier PR for #65718 is
#65721, closed by the stale bot; no other open PR references the issue.

**Test commands:**
```
pytest python/ray/serve/tests/unit/test_proxy.py   # 43 passed
pre-commit run --files python/ray/serve/_private/proxy_response_generator.py python/ray/serve/tests/unit/test_proxy.py   # all hooks pass
```

**AI assistance:** I used an AI coding assistant to write this change,
its tests and this description.

---------

Signed-off-by: Pardeep Singh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Serve] Unary proxy response timeout leaves local await task pending

3 participants