Repository navigation
[core][sandbox] Keep gRPC facade exec state on the sandbox's host - #67078
Conversation
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
| start = asyncio.ensure_future(self._start(handle, request)) | |
| start = asyncio.create_task(self._start(handle, request)) |
There was a problem hiding this comment.
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.
0c988eb to
8214469
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
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]>
8214469 to
ce47933
Compare
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]>
…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

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 acceptsnum_replicas. Readhost.pyfirst:fs_readkeeps the content, andstdin_writebuffers a write's stdin on the host (capped bymax_file_bytes) until EOF. A repeated last chunk isn't appended twice.grpc_facade.py, router calls find the sandbox by the request'stask_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_appon Serve, four 30-CPU sandbox workers on a CoreWeave cluster, real gVisor sandboxes. Medians of 3-6 runs; no sandbox failed.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
test_any_replica_serves_any_exec_callruns 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.ray[serve]and Modal SDK 1.5.5:pytest python/ray/experimental/sandbox/http/tests182 passed (master: 176), plustest_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.