Repository navigation
[serve] Import grpc_reflection only when gRPC reflection is enabled - #67444
Conversation
Since ray-project#65854, grpc_util.py imports grpc_reflection at module load, so `import ray.serve` needs grpcio-reflection even though reflection is opt-in and the package only comes with the serve extra. vLLM's Ray metrics import ray.serve, so in a ray[default] environment a vLLM engine with RayPrometheusStatLogger fails to start with "RayPrometheusMetric requires Ray to be installed." Import it in enable_server_reflection(), its only user. If reflection is turned on without the package, the error names it and points to `pip install "ray[serve]"`. Signed-off-by: xyuzh <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request makes gRPC server reflection opt-in by moving the import of grpc_reflection from the module level to inside the enable_server_reflection function, preventing import failures when grpcio-reflection is not installed. Unit tests have been added to verify this behavior. The feedback suggests raising a new ModuleNotFoundError chained with from e instead of mutating the msg attribute of the caught exception in-place, ensuring consistency with e.args.
| except ModuleNotFoundError as e: | ||
| e.msg += ( | ||
| ". gRPC server reflection needs grpcio-reflection. You can run " | ||
| '`pip install "ray[serve]"` to install all Ray Serve dependencies.' | ||
| ) | ||
| raise e |
There was a problem hiding this comment.
Mutating the msg attribute of an existing exception in-place is generally discouraged because it does not update e.args, which can lead to inconsistencies (e.g., e.args still contains the old message while str(e) contains the new one). Instead, it is more idiomatic and robust to raise a new ModuleNotFoundError with the updated message and chain it using from e to preserve the original traceback.
| except ModuleNotFoundError as e: | |
| e.msg += ( | |
| ". gRPC server reflection needs grpcio-reflection. You can run " | |
| '`pip install "ray[serve]"` to install all Ray Serve dependencies.' | |
| ) | |
| raise e | |
| except ModuleNotFoundError as e: | |
| raise ModuleNotFoundError( | |
| f"{e.msg}. gRPC server reflection needs grpcio-reflection. You can run " | |
| '`pip install "ray[serve]"` to install all Ray Serve dependencies.' | |
| ) from e |
There was a problem hiding this comment.
Applied in f728c90: it now raises a new ModuleNotFoundError chained from e, so args matches the message. test_enable_server_reflection_without_grpc_reflection still checks the message.
…ught one Keeps the error's args consistent with its message (review suggestion). Signed-off-by: xyuzh <[email protected]>
| except ModuleNotFoundError as e: | ||
| e.msg += ( | ||
| ". gRPC server reflection needs grpcio-reflection. You can run " | ||
| '`pip install "ray[serve]"` to install all Ray Serve dependencies.' | ||
| ) | ||
| raise e |
There was a problem hiding this comment.
Applied in f728c90: it now raises a new ModuleNotFoundError chained from e, so args matches the message. test_enable_server_reflection_without_grpc_reflection still checks the message.
…lection Signed-off-by: xyuzh <[email protected]>
YashwanthRanjanSingaravel
left a comment
There was a problem hiding this comment.
Looks good!
| # Imported here because reflection is opt-in: importing Serve shouldn't | ||
| # need grpcio-reflection (vLLM's Ray metrics import ray.serve, for one). |
There was a problem hiding this comment.
Nit: reflection is on by default (enable_reflection defaults to True in serve/config.py:971), so "opt-in" here is misleading.
…disk caps, egress allowlist Moves the pin to Ray master @ 78e4b1f713, which has every merged sandbox fix and ray-project/ray#67444, so vLLM's Ray metrics no longer need ray[serve] in SkyRL's env. Validated there with 64 SWE-bench Verified tasks (Harbor's oracle and SkyRL's generator). - Pin Ray to master @ 78e4b1f713 in pyproject.toml, uv.lock and the image. - dockerfile-parse in the harbor extra: the environment reads the task Dockerfile's WORKDIR with Harbor's parse_dockerfile_workdir, which imports it, and harbor[daytona,modal] doesn't install it, so every task with a Dockerfile failed in the environment's constructor. - The image installs nftables and util-linux: Ray sets up network="public" sandboxes with nft, nsenter and setpriv since ray-project/ray#66359. gVisor goes to release-20261019.0. - The task's storage_mb caps the writable rootfs (ray-project/ray#66988). - New cidr_allowlist kwarg, passed to Ray, for public sandboxes that must reach a host inside the cluster. The comment on "public" said it could reach the Ray GCS; since ray-project/ray#66359 it reaches the internet only. - black reformats lines the file already had to the repo's 120 columns. Signed-off-by: xyuzh <[email protected]>
…ay-project#67444) ## Description Since ray-project#65854, `ray/serve/_private/grpc_util.py` imports `grpc_reflection` at module load, so `import ray.serve` needs grpcio-reflection even when reflection is off. That package only comes with the `serve` extra. vLLM's Ray metrics (`vllm/v1/metrics/ray_wrappers.py`) import `ray.serve` and `ray.util.metrics` in one `try` block, so in a `ray[default]` environment a vLLM engine with `RayPrometheusStatLogger` fails to start with "RayPrometheusMetric requires Ray to be installed." I hit this on Ray nightly with SkyRL, which turns that logger on by default. Ray 2.59.0 doesn't have the import, so the next release would be the first one affected. This PR moves the import into `enable_server_reflection()`, its only user. If reflection is turned on without the package, the error names it and points to `pip install "ray[serve]"`, the hint Serve already adds at import time. ## Related issues None filed. Found while running SkyRL's Harbor integration on Ray nightly (skyrl-project/SkyRL#2467). ## Additional information - Not a duplicate: I searched PRs and issues for `grpc_reflection`, `grpcio-reflection` and the vLLM error, and none moves this import. ray-project#66067 added grpcio-reflection to the `serve` extra, which doesn't cover environments without that extra. - Tests: `test_serve_imports_without_grpc_reflection` imports Serve in a subprocess with `grpc_reflection` blocked, and `test_enable_server_reflection_without_grpc_reflection` checks the error message. Both fail on master. - Tested on macOS arm64 and Python 3.12 with today's nightly wheel (3608367, whose `grpc_util.py` matches master) and the two changed files linked in. `pytest python/ray/serve/tests/unit/test_grpc_util.py`: 18 passed. `pytest python/ray/serve/tests/test_grpc.py`: 45 passed. With grpcio-reflection uninstalled, vLLM's imports fail on master and work with this change. pre-commit passes. - I used Claude Code to help write and test this change. --------- Signed-off-by: xyuzh <[email protected]>
Description
Since #65854,
ray/serve/_private/grpc_util.pyimportsgrpc_reflectionat module load, soimport ray.serveneeds grpcio-reflection even when reflection is off. That package only comes with theserveextra. vLLM's Ray metrics (vllm/v1/metrics/ray_wrappers.py) importray.serveandray.util.metricsin onetryblock, so in aray[default]environment a vLLM engine withRayPrometheusStatLoggerfails to start with "RayPrometheusMetric requires Ray to be installed." I hit this on Ray nightly with SkyRL, which turns that logger on by default. Ray 2.59.0 doesn't have the import, so the next release would be the first one affected.This PR moves the import into
enable_server_reflection(), its only user. If reflection is turned on without the package, the error names it and points topip install "ray[serve]", the hint Serve already adds at import time.Related issues
None filed. Found while running SkyRL's Harbor integration on Ray nightly (skyrl-project/SkyRL#2467).
Additional information
grpc_reflection,grpcio-reflectionand the vLLM error, and none moves this import. [serve] Add grpcio-reflection to the serve extra #66067 added grpcio-reflection to theserveextra, which doesn't cover environments without that extra.test_serve_imports_without_grpc_reflectionimports Serve in a subprocess withgrpc_reflectionblocked, andtest_enable_server_reflection_without_grpc_reflectionchecks the error message. Both fail on master.grpc_util.pymatches master) and the two changed files linked in.pytest python/ray/serve/tests/unit/test_grpc_util.py: 18 passed.pytest python/ray/serve/tests/test_grpc.py: 45 passed. With grpcio-reflection uninstalled, vLLM's imports fail on master and work with this change. pre-commit passes.