Skip to content

[serve] Enforce type checking on the proxy/data-plane modules - #65332

Merged
abrarsheikh merged 4 commits into
ray-project:masterfrom
johntaylor-cell:serve-typecheck-proxy
Aug 11, 2026
Merged

abrarsheikh merged 4 commits into
ray-project:masterfrom
johntaylor-cell:serve-typecheck-proxy

Conversation

@johntaylor-cell

Copy link
Copy Markdown
Contributor

Add haproxy.py, proxy_state.py, and proxy.py to the mypy and pyrefly serve allowlists and fix their type errors.

  • haproxy: add the HAProxyConfig.error_file_path field, annotate the process handle and backend configs Optional, assert-narrow only where a None would already crash, honest return types for _generate_config_file_internal and pong, minimal ignores for the proc log-path monkey-patch and the async shutdown override
  • proxy_state: _get_or_create_proxy_actor returns an ActorHandle (not a ProxyWrapper), collapsing a spurious union, declare worker_id/log_file_path on the ProxyWrapper ABC, type the async-check futures Optional, assert the connection params only in the create branch, Type[ActorProxyWrapper] for the constructed wrapper
  • proxy: narrow ProxyRequest to its ASGI/gRPC subtype per method, make the GenericProxy abstract response/send methods non-async so the async-generator overrides match, AsyncGenerator return types on the streaming handlers, actor-context narrowing for the actor name/id
  • grpc_util: widen get_grpc_response_status's timeout to Optional[float]
  • pyrefly config: add jinja2 and ray_haproxy to replace-imports-with-any

Add haproxy.py, proxy_state.py, and proxy.py to the mypy + pyrefly serve
allowlists and fix their type errors.

- haproxy: add the HAProxyConfig.error_file_path field; annotate the process
  handle and backend configs Optional; assert-narrow only where a None would
  already crash; honest return types for _generate_config_file_internal and
  pong; minimal ignores for the proc log-path monkey-patch and the async
  shutdown override.
- proxy_state: _get_or_create_proxy_actor returns an ActorHandle (not a
  ProxyWrapper), collapsing a spurious union; declare worker_id/log_file_path
  on the ProxyWrapper ABC; type the async-check futures Optional; assert the
  connection params only in the create branch; Type[ActorProxyWrapper] for the
  constructed wrapper.
- proxy: narrow ProxyRequest to its ASGI/gRPC subtype per method; make the
  GenericProxy abstract response/send methods non-async so the async-generator
  overrides match; AsyncGenerator return types on the streaming handlers;
  actor-context narrowing for the actor name/id.
- grpc_util: widen get_grpc_response_status's timeout to Optional[float].
- pyrefly config: add jinja2 and ray_haproxy to replace-imports-with-any.

Signed-off-by: john.taylor <[email protected]>
@johntaylor-cell
johntaylor-cell requested a review from a team as a code owner August 10, 2026 16:47
@johntaylor-cell johntaylor-cell self-assigned this Aug 10, 2026
@johntaylor-cell johntaylor-cell added serve Ray Serve Related Issue go add ONLY when ready to merge, run all tests labels Aug 10, 2026

@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 focuses on improving type safety and static analysis compliance across Ray Serve's proxy components, including haproxy.py, proxy.py, and proxy_state.py, by adding type annotations, casting, and assertion checks. Key structural changes include updating several abstract methods in the base Proxy class from asynchronous to synchronous, refining return types, and implementing a concrete return value for the pong method. Feedback on these changes highlights two main issues: first, a potential string formatting bug in get_grpc_response_status when request_timeout_s is None, and second, the use of assert for runtime input validation in ActorProxyWrapper, which should be replaced with an explicit ValueError to prevent bypasses when Python is run with optimizations.

Comment thread python/ray/serve/_private/grpc_util.py Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 4d50ef9. Configure here.

…out message

- ActorProxyWrapper: creating a proxy no longer requires `port` (it is unused by
  _get_or_create_proxy_actor and never passed by the manager path, so the assert
  raised AssertionError on every creation -- Cursor Bugbot). Validate the required
  connection params with an explicit ValueError instead of assert so `python -O`
  cannot strip the check (Gemini).
- get_grpc_response_status: format a missing timeout as "Request timed out."
  rather than "Request timed out after Nones." (Gemini).

Signed-off-by: john.taylor <[email protected]>
The Batch-2 `assert isinstance(proxy_request, ASGIProxyRequest/gRPCProxyRequest)`
narrowing introduced a runtime nominal check the duck-typed contract never had,
breaking test_proxy's FakeProxyRequest fixtures (TestgRPCProxy/TestHTTPProxy
::test_not_found_response). Replace all 9 with `cast(...)` -- type-only narrowing,
a runtime no-op -- restoring the prior behavior while keeping mypy + pyrefly green.

Signed-off-by: john.taylor <[email protected]>
The interface declared shutdown sync while HAProxyManager implements it async
(it awaits HAProxy subprocess teardown), which required an override ignore. The
interface's sibling lifecycle methods (check_health/is_drained/update_draining)
are already async, so shutdown was the odd one out. Declare the abstract method
async and make ProxyActor's no-op override async too, then drop the ignore.

Behavior-safe: the actor's shutdown is only invoked via
`self._actor_handle.shutdown.remote()` (Ray dispatches sync/async actor methods
identically), and both proxy actors already run async event loops.

Signed-off-by: john.taylor <[email protected]>
@abrarsheikh
abrarsheikh merged commit 19cb423 into ray-project:master Aug 11, 2026
3 checks passed
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…oject#65332)

Add haproxy.py, proxy_state.py, and proxy.py to the mypy and pyrefly
serve allowlists and fix their type errors.

- haproxy: add the HAProxyConfig.error_file_path field, annotate the
process handle and backend configs Optional, assert-narrow only where a
None would already crash, honest return types for
_generate_config_file_internal and pong, minimal ignores for the proc
log-path monkey-patch and the async shutdown override
- proxy_state: _get_or_create_proxy_actor returns an ActorHandle (not a
ProxyWrapper), collapsing a spurious union, declare
worker_id/log_file_path on the ProxyWrapper ABC, type the async-check
futures Optional, assert the connection params only in the create
branch, Type[ActorProxyWrapper] for the constructed wrapper
- proxy: narrow ProxyRequest to its ASGI/gRPC subtype per method, make
the GenericProxy abstract response/send methods non-async so the
async-generator overrides match, AsyncGenerator return types on the
streaming handlers, actor-context narrowing for the actor name/id
- grpc_util: widen get_grpc_response_status's timeout to Optional[float]
- pyrefly config: add jinja2 and ray_haproxy to replace-imports-with-any

---------

Signed-off-by: john.taylor <[email protected]>
Signed-off-by: 400Ping <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…oject#65332)

Add haproxy.py, proxy_state.py, and proxy.py to the mypy and pyrefly
serve allowlists and fix their type errors.

- haproxy: add the HAProxyConfig.error_file_path field, annotate the
process handle and backend configs Optional, assert-narrow only where a
None would already crash, honest return types for
_generate_config_file_internal and pong, minimal ignores for the proc
log-path monkey-patch and the async shutdown override
- proxy_state: _get_or_create_proxy_actor returns an ActorHandle (not a
ProxyWrapper), collapsing a spurious union, declare
worker_id/log_file_path on the ProxyWrapper ABC, type the async-check
futures Optional, assert the connection params only in the create
branch, Type[ActorProxyWrapper] for the constructed wrapper
- proxy: narrow ProxyRequest to its ASGI/gRPC subtype per method, make
the GenericProxy abstract response/send methods non-async so the
async-generator overrides match, AsyncGenerator return types on the
streaming handlers, actor-context narrowing for the actor name/id
- grpc_util: widen get_grpc_response_status's timeout to Optional[float]
- pyrefly config: add jinja2 and ray_haproxy to replace-imports-with-any

---------

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

Labels

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