Repository navigation
[serve] Enforce type checking on the proxy/data-plane modules - #65332
Conversation
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]>
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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]>
…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]>
…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]>

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