Skip to content

[serve] Import grpc_reflection only when gRPC reflection is enabled - #67444

Merged
pcmoritz merged 3 commits into
ray-project:masterfrom
xyuzh:serve-lazy-grpc-reflection
Oct 27, 2026
Merged

pcmoritz merged 3 commits into
ray-project:masterfrom
xyuzh:serve-lazy-grpc-reflection

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 26, 2026

Copy link
Copy Markdown
Member

Description

Since #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. [serve] Add grpcio-reflection to the serve extra #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.

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]>
@xyuzh
xyuzh requested a review from a team as a code owner October 26, 2026 21:08

@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 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.

Comment thread python/ray/serve/_private/grpc_util.py Outdated
Comment on lines +175 to +180
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

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.

medium

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.

Suggested change
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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Oct 27, 2026
…ught one

Keeps the error's args consistent with its message (review suggestion).

Signed-off-by: xyuzh <[email protected]>

@xyuzh xyuzh left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Applied the suggestion in f728c90. test_grpc_util.py (18 passed) and the end-to-end reflection test in test_grpc.py (2 passed) pass on top of it.

Comment thread python/ray/serve/_private/grpc_util.py Outdated
Comment on lines +175 to +180
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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Oct 27, 2026
@pcmoritz
pcmoritz enabled auto-merge (squash) October 27, 2026 02:21
@github-actions
github-actions Bot disabled auto-merge October 27, 2026 16:08
@pcmoritz
pcmoritz enabled auto-merge (squash) October 27, 2026 19:02

@YashwanthRanjanSingaravel YashwanthRanjanSingaravel 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.

Looks good!

Comment on lines +171 to +172
# Imported here because reflection is opt-in: importing Serve shouldn't
# need grpcio-reflection (vLLM's Ray metrics import ray.serve, for one).

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.

Nit: reflection is on by default (enable_reflection defaults to True in serve/config.py:971), so "opt-in" here is misleading.

@pcmoritz
pcmoritz merged commit 78e4b1f into ray-project:master Oct 27, 2026
7 checks passed
xyuzh added a commit to xyuzh/SkyRL that referenced this pull request Oct 28, 2026
…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]>
justinyeh1995 pushed a commit to justinyeh1995/ray that referenced this pull request Oct 28, 2026
…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]>
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.

3 participants