Skip to content

[serve] Correct the DeploymentResponse.cancel docstring's exception - #67531

Merged
akyang-anyscale merged 1 commit into
masterfrom
doc-1911-serve-cancel-docstring
Oct 29, 2026
Merged

akyang-anyscale merged 1 commit into
masterfrom
doc-1911-serve-cancel-docstring

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Summary

Corrects the exception that the DeploymentResponse.cancel docstring says later operations raise after a successful cancel. This is a docstring-only change. Behavior is unchanged.

The docstring sits on _DeploymentResponseBase, so DeploymentResponseGenerator.cancel inherits the fix.

Before

  • If the request was cancelled before assignment, they'll raise asyncio.CancelledError (or a concurrent.futures.CancelledError for synchronous methods like .result().).
  • If the request was cancelled after assignment, they'll raise ray.exceptions.TaskCancelledError.

A reader following this writes except concurrent.futures.CancelledError: around .result(), or except asyncio.CancelledError: around await response. Neither clause catches what Serve raises.

What the code does

Every path raises ray.serve.exceptions.RequestCancelledError, which subclasses TaskCancelledError (exceptions.py#L101):

  • Synchronous, before assignment. _fetch_future_result_sync catches the cancelled routing future's concurrent.futures.CancelledError and raises RequestCancelledError (handle.py#L394-L395).
  • Synchronous, after assignment. ActorReplicaResult._process_response converts TaskCancelledError to RequestCancelledError (replica_result.py#L159-L165). The gRPC replica result does the same for concurrent.futures.CancelledError (replica_result.py#L388-L389).
  • await, either side. The async path surfaces an asyncio.CancelledError internally (replica_result.py#L172), and DeploymentResponse.__await__ re-raises it as RequestCancelledError when the response was cancelled (handle.py#L548-L556). DeploymentResponseGenerator.__anext__ does the same (handle.py#L715-L721).

test_handle_cancellation.py already asserts RequestCancelledError for all four cases: sync and async, during assignment and during execution (L17, L46, L82, L118).

After

If the request is successfully cancelled, subsequent operations on the response raise ray.serve.exceptions.RequestCancelledError. This is the same whether the request was cancelled before or after it was assigned to a replica, and whether you await the response or call a synchronous method such as .result(). RequestCancelledError is a subclass of ray.exceptions.TaskCancelledError, so except TaskCancelledError also catches it.

Subsystems

Serve (docstrings only)

Not a duplicate

I searched open PRs for DeploymentResponse cancel, RequestCancelledError, and Serve handle.py docstring changes. #66643 edits handle.py to let a handler claim an unrouted call, and #67109 and #67210 change cancellation handling in the proxy and gRPC stream paths. None of them changes this docstring or the exception the public cancel path raises.

Testing

  • pre-commit run --files python/ray/serve/handle.py passes, including ruff, black, pydoclint, mypy and pyrefly for Serve, and the Ray docstyle check.
  • I read each path above on master at 30ca1d1.
  • I ran a probe script against the released ray[serve]==2.59.0 wheel, whose cancellation paths match master. It cancels a request queued behind a full replica (max_ongoing_requests=1) and one already running on it, from a driver with .result() and from inside a deployment with await. All four raised ray.serve.exceptions.RequestCancelledError, and none was an instance of asyncio.CancelledError or concurrent.futures.CancelledError.
  • I didn't run the Serve test suite. The existing tests linked above cover the behavior this docstring now describes.

The docstring-only label is applied to skip the heavy Serve test suites, and the commit carries an @rtd-build line to build the API reference preview.

I wrote this with AI assistance (Claude Code). I reviewed every changed line and can defend the change.

🤖 Generated with Claude Code

The cancel docstring said a request cancelled before replica assignment
raises asyncio.CancelledError, or concurrent.futures.CancelledError from
synchronous methods such as .result(), and that one cancelled after
assignment raises TaskCancelledError. Every path raises
RequestCancelledError, a TaskCancelledError subclass:
_fetch_future_result_sync, the replica result's _process_response, and
__await__ all convert the cancellation. test_handle_cancellation.py
already asserts this for all four cases.

Docstring-only change.

@rtd-build

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman dstrodtman added serve Ray Serve Related Issue docs An issue or change related to documentation go add ONLY when ready to merge, run all tests docstring-only Skip heavy library tests for Ray API docstring edits. labels Oct 29, 2026
@dstrodtman
dstrodtman requested a review from a team as a code owner October 29, 2026 13:23

@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 updates the docstring of the cancel method in python/ray/serve/handle.py to clarify that ray.serve.exceptions.RequestCancelledError is raised when a request is successfully cancelled, regardless of whether it was cancelled before or after assignment. There are no review comments to evaluate, and no additional feedback is provided.

@akyang-anyscale
akyang-anyscale merged commit fd116c6 into master Oct 29, 2026
7 checks passed
@akyang-anyscale
akyang-anyscale deleted the doc-1911-serve-cancel-docstring branch October 29, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs An issue or change related to documentation docstring-only Skip heavy library tests for Ray API docstring edits. go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants