Skip to content

[core][sandbox] Serve the gRPC facade with Ray Serve - #66820

Merged
pcmoritz merged 5 commits into
ray-project:masterfrom
xyuzh:sandbox-grpc-serve-app
Oct 12, 2026
Merged

pcmoritz merged 5 commits into
ray-project:masterfrom
xyuzh:sandbox-grpc-serve-app

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Description

Lets Ray Serve deploy the Modal-compatible gRPC facade (#65839), so a Serve application, such as an Anyscale service, exposes the facade from configuration alone. The facade itself becomes a standard grpc servicer, which both its own server and Serve's gRPC proxy call:

  • Each RPC is a method named after it that takes the request and the call's context (as grpc_context, the name Serve passes it under) and returns or yields its replies. The vendored service stubs are now grpcio's (*_pb2_grpc.py, from the same pinned grpcio-tools), so the facade no longer needs grpclib, and python -m ray.experimental.sandbox.http.grpc_facade runs a grpc.aio server.
  • RaySandboxFacade serves both services over one state. One decorator checks the API token ([core][sandbox] Require the API token on the gRPC facade #66675) before a handler runs and ends a failed call with its status: through abort() on grpc.aio, and on Serve, whose context has no abort(), by setting the status on the context and raising.
  • grpc_facade.add_servicers_to_server, for grpc_options.grpc_servicer_functions, registers the client SDK's two services under their wire names.
  • grpc_app.build_app builds a one-replica deployment of the facade's servicer. build_app refuses to build without RAY_SANDBOX_API_TOKEN, and the replica refuses to start without the token in its own environment, which can lack what build_app saw (as with serve run outside the cluster). Serve's proxies listen on every node's address, which sandboxes with network access can reach.
  • Without a configured advertise_url, the facade gives each client https:// plus the host it dialed (x-modal-host) as its command-router URL, which suits a TLS endpoint in front of the proxy.
  • Docs: a "Serve the facade with Ray Serve" section in doc/source/core/sandboxes.md, with the Anyscale service config, and two notes about clients:
    • Client SDK 1.6+ needs MODAL_SANDBOX_V2=0, because 1.6 made a newer sandbox API its default and the facade doesn't serve it. The client SDK tests set it too.
    • The TLS endpoint in front of the facade must accept HTTP/2 without ALPN, because the SDK's command-router connection offers none. Envoy does; ingress-nginx doesn't. So on Anyscale, the service belongs on a Kubernetes cloud that routes services through an Envoy Gateway.

Related issues

Related to #65839 (the facade) and #66675 (its API token). #66821, stacked on this one, makes the facade fast under bursts of sandboxes.

Additional information

  • Not a duplicate: no open PR serves the facade through Ray Serve. I searched open PRs for grpc_app, "sandbox facade Serve" and "sandbox gRPC facade".
  • Tests:
    • pytest python/ray/experimental/sandbox/http/tests on macOS with the Ray 2.58.0 wheel (with Serve and the Modal SDK 1.6.1 installed) and this branch's ray.experimental.sandbox: 157 passed, 2 skipped (the two TEST_SANDBOX=1 integration tests). This includes the unmodified SDK against both the facade's grpc.aio server and a local Ray Serve gRPC proxy: sandbox create, exec, a multi-message file-write stream, and a rejected token.
    • With CI's pins (grpcio 1.75.0, protobuf 5.27.5) and without grpclib, Serve or modal installed: test_grpc_facade.py, test_grpc_app.py and test_modal_client.py gave 70 passed, 9 skipped (the Serve and SDK end-to-end tests). The facade's wire tests no longer need grpclib, so CI now runs them instead of skipping them.
    • pre-commit run --files <changed files> with pre-commit 3.5.0 (every hook that applies, including both ruff hooks, pydoclint and black) and the API parameter coverage check (ci/ray_ci/doc/cmd_check_api_param_coverage.py --base-ref origin/master --blocking) pass.
  • End to end with the stock Modal SDK (1.6.1, no patch), before the facade became a grpc servicer (the first revision served it through an adapter in grpc_app): this app ran as an Anyscale staging service on a Kubernetes cloud whose edge is Envoy Gateway.
    • The SDK created sandboxes, ran commands in them (exit code, stdout, stderr, line streaming, secrets, workdir), wrote and read files, reattached, and terminated them.
    • Uploads and downloads of up to 64 MiB passed, and so did reading a command's output before wait() while it stayed silent for 720 s.
    • A command-router call over TLS without ALPN reached the facade, and calls without the token got UNAUTHENTICATED.
    • Behind ingress-nginx, the same SDK creates sandboxes, but its command-router calls fail, as the docs now say.
  • The burst benchmarks in [core][sandbox] Make the gRPC facade fast under bursts of sandboxes #66821 also ran this app as an Anyscale service, on a 16-node EKS staging cluster behind ingress-nginx, with a one-line ALPN fix applied to the client SDK.
  • AI assistance (Claude Code) was used to prepare this change.

Add ray.experimental.sandbox.http.grpc_app, which serves the gRPC facade
through Ray Serve's gRPC proxy, so a Serve application such as an Anyscale
service exposes it from configuration alone:

- add_servicers_to_server, for grpc_options.grpc_servicer_functions,
  registers the client SDK's two services under their wire names. Their
  method tables come from the generated stubs that the facade's own server
  routes with.
- build_app builds a one-replica ingress whose methods run the facade's
  handlers through the table that server dispatches through, so the API
  token check applies unchanged. It refuses to build without a token, and
  the replica refuses to start without one in its own environment, which
  can lack what build_app saw (as with `serve run` outside the cluster):
  Serve's proxies listen on every node's address, which sandboxes with
  network access can reach.
- Without a configured advertise_url, the facade hands each client
  https:// plus the host it dialed (x-modal-host) as its command-router
  URL, which suits a TLS endpoint in front of the proxy.

The client SDK tests set MODAL_SANDBOX_V2=0: modal 1.6 made a newer
sandbox API, which the facade doesn't serve, its default.

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

@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 introduces a Ray Serve application (grpc_app.py) to serve the sandbox gRPC facade through Ray Serve's gRPC proxy, enabling deployment as a Serve application. It also updates the gRPC facade to dynamically derive the command-router URL from the client's dialed host (x-modal-host header) when no explicit advertise_url is configured. Feedback on the changes suggests normalizing metadata keys to lowercase in grpc_app.py to prevent potential authentication or precondition failures due to mixed-case headers.

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue docs An issue or change related to documentation labels Oct 8, 2026
@xyuzh

xyuzh commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

@pcmoritz

Comment thread doc/source/core/sandboxes.md Outdated
import_path: ray.experimental.sandbox.http.grpc_app:build_app
```

Client SDK releases through 1.6.1 don't negotiate HTTP/2 with ALPN on their command-router connection. A TLS endpoint that requires it, such as an Anyscale service's, refuses those calls, so creating and terminating sandboxes works but running commands and file operations fail.

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.

This should be resolved before merging

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.

Resolved without changing the client. The SDK's command-router connection offers no ALPN; Envoy accepts it because it detects HTTP/2 from the connection preface, while nginx switches a TLS connection to HTTP/2 only through ALPN. On a staging service behind Envoy Gateway, the stock modal 1.6.1 with no patch passed create, exec, file round trips, reattach and terminate, 64 MiB uploads, a command silent for 720 s read before wait(), and a router call over TLS without ALPN. The docs now say this (29b8545).

Comment thread doc/source/core/sandboxes.md Outdated

Client SDK releases through 1.6.1 don't negotiate HTTP/2 with ALPN on their command-router connection. A TLS endpoint that requires it, such as an Anyscale service's, refuses those calls, so creating and terminating sandboxes works but running commands and file operations fail.

A proxy in front of the facade also limits each HTTP/2 stream. The client SDK sends a file write as one request stream, and it reads a command's output on a stream that stays quiet until the command exits. ingress-nginx, for example, defaults to 1 MiB per request (`proxy-body-size`) and 60 seconds without data from the backend (`proxy-read-timeout`). Behind those defaults, file writes larger than about 7.5 MiB fail, and so do output reads that wait on a running command for more than about 11 minutes, once the SDK's retries run out. Raise both limits on the proxy for the facade's routes.

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.

For limitations like this it would be good to describe exactly how nginx should be configured to avoid it (e.g. give a small snippet)

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.

nginx can't be configured to avoid the command-router failure: it switches a TLS connection to HTTP/2 only through ALPN, and the SDK offers none. So the docs now point at Envoy and give the exact Envoy Gateway policy for the stream timeout (the one on the staging Gateway where the stock SDK passed), plus the ingress-nginx annotations for its body and read limits (f1791ac).

@@ -0,0 +1,302 @@
"""Ray Serve application for the gRPC facade.

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.

Instead of introducing these wrappers, can we refactor the current grpc_facade to make it compatible with Ray Serve instead?

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.

Done in b2dc400: the facade is now a standard grpc servicer, so Serve calls it directly and the wrappers are gone.

  • Each RPC is a method named after it that takes (request, grpc_context) and returns its reply, or yields replies for a streaming RPC. A grpc.aio server and Serve's gRPC proxy both call that convention.
  • The vendored service stubs are grpcio's (*_pb2_grpc.py), so the facade no longer needs grpclib. python -m ray.experimental.sandbox.http.grpc_facade runs a grpc.aio server, and Serve configs list grpc_facade.add_servicers_to_server.
  • One decorator checks the token and turns the facade's errors into the call's status: abort() on grpc.aio, and on Serve, whose context has no abort(), the status set on the context plus a raise.
  • grpc_app.py went from 302 to 124 lines. Its ingress subclasses RaySandboxFacade only to check the token in the replica's environment, build the resolver there, and answer HTTP with a 404.

The facade's wire tests now use grpc.aio clients, so CI runs them without grpclib. Locally, the unmodified Modal SDK passes against both the grpc.aio server and a local Serve proxy.

xyuzh added 3 commits October 9, 2026 12:45
…d router

The client SDK's command-router connection offers no ALPN and speaks HTTP/2
anyway. Envoy detects HTTP/2 from the connection preface and accepts it;
ingress-nginx doesn't. Say so, point Anyscale deployments at clouds that route
services through an Envoy Gateway, and note Envoy's default route timeout,
which ends long output streams.

Signed-off-by: xyuzh <[email protected]>
Give the Envoy Gateway policy that turns off Envoy's default route timeout,
which ends a command's output stream after 15 seconds, and name the
ingress-nginx annotations that raise its body and read limits.

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

The facade now follows the standard grpc servicer convention, which both a
grpc.aio server and Ray Serve's gRPC proxy call: each RPC is a method named
after it that takes the request and the call's context (as `grpc_context`,
the name Serve passes it under) and returns or yields its replies. This
replaces grpc_app's adapter, which emulated grpclib streams on Serve.

- The vendored service stubs are grpcio's (`*_pb2_grpc.py`, from the same
  pinned grpcio-tools), so the facade no longer needs grpclib.
- `RaySandboxFacade` serves both services over one state. `_rpc` checks the
  token before a handler runs and ends the call with an `_RpcError`'s
  status: through abort() on grpc.aio, and on Serve, whose context has no
  abort(), by setting the status on the context and raising.
- `python -m ray.experimental.sandbox.http.grpc_facade` runs a grpc.aio
  server.
- grpc_app's ingress is `RaySandboxFacade` itself, and Serve configs name
  `grpc_facade.add_servicers_to_server`.
- The facade tests use grpc.aio clients, so they run without grpclib.

Signed-off-by: xyuzh <[email protected]>
@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Oct 9, 2026
Read the Docs build 4551504 hung in the Sphinx step and was terminated for inactivity; the same commits built on ray-project#66821. This commit changes no files.

Signed-off-by: xyuzh <[email protected]>
@pcmoritz
pcmoritz merged commit 6817455 into ray-project:master Oct 12, 2026
6 checks passed
xyuzh added a commit to xyuzh/SkyRL that referenced this pull request Oct 13, 2026
Ray Sandbox serves a Modal-compatible gRPC API (ray-project/ray#66820,
#66821), so Harbor's existing `modal` environment can run its sandboxes on
Ray instead of Modal's cloud with the stock Modal SDK.

- prepare_runtime_environment forwards MODAL_SERVER_URL to Ray workers, so
  the generator's Modal SDK uses the configured server.
- examples/train_integrations/harbor/ray_sandbox runs run_harbor_gen.sh's
  generation that way, with sandboxes colocated in the job's cluster or in
  a separate Anyscale service, and reports results up to 128 concurrent
  sandboxes.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/SkyRL that referenced this pull request Oct 13, 2026
Ray Sandbox serves a Modal-compatible gRPC API (ray-project/ray#66820,
#66821), so Harbor's existing `modal` environment can run its sandboxes on
Ray instead of Modal's cloud with the stock Modal SDK.

- prepare_runtime_environment forwards MODAL_SERVER_URL to Ray workers, so
  the generator's Modal SDK uses the configured server.
- examples/train_integrations/harbor/ray_sandbox runs run_harbor_gen.sh's
  generation that way, with sandboxes colocated in the job's cluster or in
  a separate Anyscale service, and reports results up to 128 concurrent
  sandboxes.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/SkyRL that referenced this pull request Oct 14, 2026
Ray Sandbox serves a Modal-compatible gRPC API (ray-project/ray#66820,
#66821), so Harbor's existing `modal` environment can run its sandboxes on
Ray instead of Modal's cloud with the stock Modal SDK.

- prepare_runtime_environment forwards MODAL_SERVER_URL to Ray workers, so
  the generator's Modal SDK uses the configured server.
- examples/train_integrations/harbor/ray_sandbox runs run_harbor_gen.sh's
  generation that way, with sandboxes colocated in the job's cluster or in
  a separate Anyscale service, and reports results up to 128 concurrent
  sandboxes.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/SkyRL that referenced this pull request Oct 14, 2026
Ray Sandbox serves a Modal-compatible gRPC API (ray-project/ray#66820), so
Harbor's existing `modal` environment can run its sandboxes on Ray instead of
Modal's cloud with the stock Modal SDK.

- prepare_runtime_environment forwards MODAL_SERVER_URL to Ray workers, so
  the generator's Modal SDK uses the configured server.
- examples/train_integrations/harbor/ray_sandbox runs run_harbor_gen.sh's
  generation that way, with sandboxes colocated in the job's cluster or in
  a separate Anyscale service, and reports end-to-end and sandbox benchmark
  results up to 512 concurrent sandboxes.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Oct 15, 2026
Since ray-project#66820, the gRPC facade usually runs as a Serve application whose
settings come from its args, so name that field there. The environment
variable only fills the default where the settings are built, which for
a Serve application is where it's built, not where its replica runs.

Signed-off-by: xyuzh <[email protected]>
pawelbeza pushed a commit to pawelbeza/ray that referenced this pull request Oct 23, 2026
## Description

Lets Ray Serve deploy the Modal-compatible gRPC facade (ray-project#65839), so a
Serve application, such as an Anyscale service, exposes the facade from
configuration alone. The facade itself becomes a standard `grpc`
servicer, which both its own server and Serve's gRPC proxy call:

- Each RPC is a method named after it that takes the request and the
call's context (as `grpc_context`, the name Serve passes it under) and
returns or yields its replies. The vendored service stubs are now
grpcio's (`*_pb2_grpc.py`, from the same pinned grpcio-tools), so the
facade no longer needs grpclib, and `python -m
ray.experimental.sandbox.http.grpc_facade` runs a `grpc.aio` server.
- `RaySandboxFacade` serves both services over one state. One decorator
checks the API token (ray-project#66675) before a handler runs and ends a failed
call with its status: through `abort()` on `grpc.aio`, and on Serve,
whose context has no `abort()`, by setting the status on the context and
raising.
- `grpc_facade.add_servicers_to_server`, for
`grpc_options.grpc_servicer_functions`, registers the client SDK's two
services under their wire names.
- `grpc_app.build_app` builds a one-replica deployment of the facade's
servicer. `build_app` refuses to build without `RAY_SANDBOX_API_TOKEN`,
and the replica refuses to start without the token in its own
environment, which can lack what `build_app` saw (as with `serve run`
outside the cluster). Serve's proxies listen on every node's address,
which sandboxes with network access can reach.
- Without a configured `advertise_url`, the facade gives each client
`https://` plus the host it dialed (`x-modal-host`) as its
command-router URL, which suits a TLS endpoint in front of the proxy.
- Docs: a "Serve the facade with Ray Serve" section in
`doc/source/core/sandboxes.md`, with the Anyscale service config, and
two notes about clients:
- Client SDK 1.6+ needs `MODAL_SANDBOX_V2=0`, because 1.6 made a newer
sandbox API its default and the facade doesn't serve it. The client SDK
tests set it too.
- The TLS endpoint in front of the facade must accept HTTP/2 without
ALPN, because the SDK's command-router connection offers none. Envoy
does; ingress-nginx doesn't. So on Anyscale, the service belongs on a
Kubernetes cloud that routes services through an Envoy Gateway.

## Related issues

Related to ray-project#65839 (the facade) and ray-project#66675 (its API token). ray-project#66821,
stacked on this one, makes the facade fast under bursts of sandboxes.

## Additional information

- Not a duplicate: no open PR serves the facade through Ray Serve. I
searched open PRs for `grpc_app`, "sandbox facade Serve" and "sandbox
gRPC facade".
- Tests:
- `pytest python/ray/experimental/sandbox/http/tests` on macOS with the
Ray 2.58.0 wheel (with Serve and the Modal SDK 1.6.1 installed) and this
branch's `ray.experimental.sandbox`: 157 passed, 2 skipped (the two
`TEST_SANDBOX=1` integration tests). This includes the unmodified SDK
against both the facade's `grpc.aio` server and a local Ray Serve gRPC
proxy: sandbox create, exec, a multi-message file-write stream, and a
rejected token.
- With CI's pins (grpcio 1.75.0, protobuf 5.27.5) and without grpclib,
Serve or modal installed: `test_grpc_facade.py`, `test_grpc_app.py` and
`test_modal_client.py` gave 70 passed, 9 skipped (the Serve and SDK
end-to-end tests). The facade's wire tests no longer need grpclib, so CI
now runs them instead of skipping them.
- `pre-commit run --files <changed files>` with pre-commit 3.5.0 (every
hook that applies, including both ruff hooks, pydoclint and black) and
the API parameter coverage check
(`ci/ray_ci/doc/cmd_check_api_param_coverage.py --base-ref origin/master
--blocking`) pass.
- End to end with the stock Modal SDK (1.6.1, no patch), before the
facade became a `grpc` servicer (the first revision served it through an
adapter in `grpc_app`): this app ran as an Anyscale staging service on a
Kubernetes cloud whose edge is Envoy Gateway.
- The SDK created sandboxes, ran commands in them (exit code, stdout,
stderr, line streaming, secrets, workdir), wrote and read files,
reattached, and terminated them.
- Uploads and downloads of up to 64 MiB passed, and so did reading a
command's output before `wait()` while it stayed silent for 720 s.
- A command-router call over TLS without ALPN reached the facade, and
calls without the token got UNAUTHENTICATED.
- Behind ingress-nginx, the same SDK creates sandboxes, but its
command-router calls fail, as the docs now say.
- The burst benchmarks in ray-project#66821 also ran this app as an Anyscale
service, on a 16-node EKS staging cluster behind ingress-nginx, with a
one-line ALPN fix applied to the client SDK.
- AI assistance (Claude Code) was used to prepare 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

docs An issue or change related to documentation go add ONLY when ready to merge, run all tests sandbox serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants