Repository navigation
Conversation
Signed-off-by: Henry Su <[email protected]>
There was a problem hiding this comment.
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.
| 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() |
There was a problem hiding this comment.
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.
| 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() |
|
This pull request has been automatically marked as stale because it has not had 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. |
|
This pull request has been automatically closed because there has been no more activity in the 14 days 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! |
| return wait_for_response().__await__() | ||
|
|
||
| response = Response() | ||
| gen = ProxyResponseGenerator(response, timeout_s=0.001) |
There was a problem hiding this comment.
please rewrite the test without using ProxyResponseGenerator since its a private module
|
Could you make the change requested by @abrarsheikh? Thanks |
…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]>
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.pyruff check python/ray/serve/_private/proxy_response_generator.py python/ray/serve/tests/test_proxy_response_generator.pygit diff --checkray._rayletmodule is not installed.