Skip to content

[core][sandbox] Keep gRPC facade exec state on the sandbox's host - #67078

Merged
pcmoritz merged 2 commits into
ray-project:masterfrom
xyuzh:sandbox-facade-stateless
Oct 29, 2026
Merged

pcmoritz merged 2 commits into
ray-project:masterfrom
xyuzh:sandbox-facade-stateless

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Description

The gRPC facade keeps every exec (a command, or an emulated file read or write) in an in-memory table, so its Serve application runs exactly one replica, and every exec-plane call goes through that replica's event loop: 1,328 execs/s for 200 sandboxes in the run below.

This PR moves exec state to each sandbox's SandboxHost, so any replica can serve any call and the application accepts num_replicas. Read host.py first:

  • Execs are keyed by the client's exec id, so a retried start (the SDK retries UNAVAILABLE) joins the first one from any replica, and a command never runs twice.
  • File reads and writes are host jobs: fs_read keeps the content, and stdin_write buffers a write's stdin on the host (capped by max_file_bytes) until EOF. A repeated last chunk isn't appended twice.
  • In grpc_facade.py, router calls find the sandbox by the request's task_id, which the SDK always sends (the vendored proto now declares it), and try the resolver's handle cache before a worker thread.

A finished exec now stays readable while its sandbox's exec history keeps it (max_exec_history, 256 by default) instead of for the facade's 120 s.

Measured on 2026-10-14 against base 00a3adb: unmodified Modal SDK 1.6.1 clients against grpc_app:build_app on Serve, four 30-CPU sandbox workers on a CoreWeave cluster, real gVisor sandboxes. Medians of 3-6 runs; no sandbox failed.

creates (N=400), ready/s at t99 execs/s, 200 sandboxes x 30
master (1 replica) 102 1,328
This PR, 1 replica 111 1,557
This PR, 4 replicas 111 2,756

Creates don't change: they're bound by starting a Ray worker per sandbox.

Related issues

Split out of #66821. Node hosts, shared placement groups (slabs), node-host warm pools and a direct host channel (B2-B5 of the split) follow as separate PRs stacked on this one.

Additional information

  • Not a duplicate: no open PR other than [core][sandbox] Make the gRPC facade fast under bursts of sandboxes #66821 changes the facade's exec state or replica count (I searched for "sandbox facade replicas" and "sandbox exec state").
  • Tests: test_any_replica_serves_any_exec_call runs two facades on one sandbox: a start retried on the other one runs once, and stdin, stdout and wait work across them. It fails on master, where the retry runs the command again. Six host tests cover exec keys, stdin chunks and caps, failing file jobs and terminate.
  • Tested rebased on master a82c56e (before [core][sandbox] Enforce CPU and memory limits with host cgroups #66932 merged; macOS unit tests and CI cover the rebase onto it). On macOS with the Ray 2.58.0 wheel, ray[serve] and Modal SDK 1.5.5: pytest python/ray/experimental/sandbox/http/tests 182 passed (master: 176), plus test_full_sandbox_lifecycle, which needs gVisor. In a privileged CoreWeave pod with real gVisor (runsc release-20261012.0, TEST_SANDBOX=1): the HTTP tests 173 passed, 10 skipped (master: 167 passed, 10 skipped; the skips need modal). pre-commit passes.
  • I used Claude Code to help write and test this change.

@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 refactors the Ray Sandbox gRPC facade to be completely stateless, allowing multiple replicas of the facade to run concurrently. Exec state management (including file operations and stdin buffering) has been moved from the facade's in-memory tables to the individual sandbox hosts, and task_id (sandbox ID) has been added to the relevant gRPC request messages to facilitate routing. The feedback suggests using the more modern and idiomatic asyncio.create_task instead of asyncio.ensure_future when scheduling coroutines.

# stops waiting (UNAVAILABLE below, a dropped connection): the host
# joins a retried start with the same exec id to this one, so the
# command never runs twice.
start = asyncio.ensure_future(self._start(handle, request))

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

In modern Python (3.7+), asyncio.create_task is the preferred and more idiomatic API for scheduling a coroutine as a task on the event loop, rather than asyncio.ensure_future.

Suggested change
start = asyncio.ensure_future(self._start(handle, request))
start = asyncio.create_task(self._start(handle, request))

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.

Not changing this: it's the call master already makes here (record.start_task = asyncio.ensure_future(...)), and for a coroutine ensure_future just calls create_task. I'd rather keep this PR's diff to moving the exec state.

@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Oct 15, 2026
@xyuzh
xyuzh force-pushed the sandbox-facade-stateless branch from 0c988eb to 8214469 Compare October 22, 2026 19:40

@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 and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 8214469. Configure here.

The gRPC facade kept every exec in an in-memory table, so Serve could
run only one replica of it. Each exec now lives on its sandbox's
SandboxHost under the client's exec id: a retried start joins the first,
and file reads and writes (the SDK's fs-tools commands) are host jobs
whose content and buffered stdin stay on the host. The router requests
already carry the sandbox's task id, now declared in the vendored proto,
so any replica can serve any call, and the Serve application accepts
num_replicas.

Router calls look sandboxes up from the resolver's handle cache first,
so a replica doesn't spend a worker thread per call on handles it holds.

Signed-off-by: xyuzh <[email protected]>
@xyuzh
xyuzh force-pushed the sandbox-facade-stateless branch from 8214469 to ce47933 Compare October 22, 2026 22:05
The final stdin write of a file upload waited for the host to write the
whole file, but the facade bounds that one call at _WRITE_BOUND_SECONDS.
A large file over a slow sandbox then failed with UNAVAILABLE, and the
client's retry failed with offset_mismatch, since the host had already
accepted every byte. Before this PR, the facade bounded each 4 MiB slice
on its own.

The host now starts the write and returns, and the job finishes when the
file is written. Clients already wait on the job's exit code for the
write's result.

Signed-off-by: xyuzh <[email protected]>
@pcmoritz pcmoritz added sandbox go add ONLY when ready to merge, run all tests labels Oct 28, 2026
@pcmoritz
pcmoritz enabled auto-merge (squash) October 29, 2026 21:41
@pcmoritz
pcmoritz merged commit eca2e48 into ray-project:master Oct 29, 2026
8 checks passed
pawelbeza pushed a commit to pawelbeza/ray that referenced this pull request Nov 2, 2026
…y-project#67078)

## Description

The gRPC facade keeps every exec (a command, or an emulated file read or
write) in an in-memory table, so its Serve application runs exactly one
replica, and every exec-plane call goes through that replica's event
loop: 1,328 execs/s for 200 sandboxes in the run below.

This PR moves exec state to each sandbox's `SandboxHost`, so any replica
can serve any call and the application accepts `num_replicas`. Read
`host.py` first:

- Execs are keyed by the client's exec id, so a retried start (the SDK
retries UNAVAILABLE) joins the first one from any replica, and a command
never runs twice.
- File reads and writes are host jobs: `fs_read` keeps the content, and
`stdin_write` buffers a write's stdin on the host (capped by
`max_file_bytes`) until EOF. A repeated last chunk isn't appended twice.
- In `grpc_facade.py`, router calls find the sandbox by the request's
`task_id`, which the SDK always sends (the vendored proto now declares
it), and try the resolver's handle cache before a worker thread.

A finished exec now stays readable while its sandbox's exec history
keeps it (`max_exec_history`, 256 by default) instead of for the
facade's 120 s.

Measured on 2026-10-14 against base 00a3adb: unmodified Modal SDK
1.6.1 clients against `grpc_app:build_app` on Serve, four 30-CPU sandbox
workers on a CoreWeave cluster, real gVisor sandboxes. Medians of 3-6
runs; no sandbox failed.

| | creates (N=400), ready/s at t99 | execs/s, 200 sandboxes x 30 |
| --- | --- | --- |
| master (1 replica) | 102 | 1,328 |
| This PR, 1 replica | 111 | 1,557 |
| This PR, 4 replicas | 111 | 2,756 |

Creates don't change: they're bound by starting a Ray worker per
sandbox.

## Related issues

Split out of ray-project#66821. Node hosts, shared placement groups (slabs),
node-host warm pools and a direct host channel (B2-B5 of the split)
follow as separate PRs stacked on this one.

## Additional information

- Not a duplicate: no open PR other than ray-project#66821 changes the facade's
exec state or replica count (I searched for "sandbox facade replicas"
and "sandbox exec state").
- Tests: `test_any_replica_serves_any_exec_call` runs two facades on one
sandbox: a start retried on the other one runs once, and stdin, stdout
and wait work across them. It fails on master, where the retry runs the
command again. Six host tests cover exec keys, stdin chunks and caps,
failing file jobs and terminate.
- Tested rebased on master a82c56e (before ray-project#66932 merged; macOS unit
tests and CI cover the rebase onto it). On macOS with the Ray 2.58.0
wheel, `ray[serve]` and Modal SDK 1.5.5: `pytest
python/ray/experimental/sandbox/http/tests` 182 passed (master: 176),
plus `test_full_sandbox_lifecycle`, which needs gVisor. In a privileged
CoreWeave pod with real gVisor (runsc release-20261012.0,
`TEST_SANDBOX=1`): the HTTP tests 173 passed, 10 skipped (master: 167
passed, 10 skipped; the skips need modal). pre-commit passes.
- I used Claude Code to help write and test this change.

---------

Signed-off-by: xyuzh <[email protected]>
Signed-off-by: Pawel Beza <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 4b9df5c4-6ec3-4e1f-ac3e-54f320ebd8c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests sandbox

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants