Skip to content

[core][sandbox] Start fewer processes per sandbox exec and file write - #67876

Merged
pcmoritz merged 1 commit into
ray-project:masterfrom
pawelbeza:pawelbeza/combine-stdout-stderr
Nov 7, 2026
Merged

pcmoritz merged 1 commit into
ray-project:masterfrom
pawelbeza:pawelbeza/combine-stdout-stderr

Conversation

@pawelbeza

@pawelbeza pawelbeza commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR reduces the work needed to execute commands and write files inside gVisor sandboxes.

  • Merged command output (opt-in): Adds combine_output=True to exec and exec_async. Stdout and stderr share one pipe, and ExecResult.output contains the merged raw bytes without UTF-8 decoding. Callers can replace a temporary-file/base64 wrapper with a direct command. The option defaults to False; ordinary direct exec still starts one process.
  • Faster file writes (automatic): Non-append writes use one tee process when the parent directory exists, preserving existing permissions, ownership, symlinks, and hardlinks. Missing directories or unavailable tee use the existing shell fallback. Append operations retain the existing shell path.

Additional information

Median latency over 40 paired runs:

Operation Before After
Temporary-file/base64 wrapper → direct merged exec 43.4 ms 13.6 ms
1 KiB file write (existing parent directory) 34.3 ms 13.8 ms

@pawelbeza
pawelbeza requested a review from a team as a code owner November 5, 2026 20:24

@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 combine_output option to the sandbox execution backend, allowing stdout and stderr to be returned together as raw bytes in ExecResult.output. It also optimizes the write_file method in the gVisor backend by using GNU install for non-append writes to avoid spawning a shell. However, the review feedback points out that using install -D introduces behavioral regressions, such as destroying symlinks and overwriting existing file permissions (e.g., resetting executable permissions to 0644). It is recommended to revert to the shell-based approach or find an alternative that preserves symlinks and permissions.

@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 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b8ddb1c. Configure here.

@pawelbeza
pawelbeza force-pushed the pawelbeza/combine-stdout-stderr branch from b8ddb1c to 90e8fda Compare November 5, 2026 21:01
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Nov 6, 2026
Add opt-in combine_output=True to return stdout and stderr together
as raw bytes, avoiding temporary-file/base64 wrappers. Default exec
behavior remains unchanged.

Use install -D for non-append file writes, reducing sandbox processes
from four to one. Fall back to the existing shell path if install fails;
append writes keep the existing path.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: e497cab1-3277-4bd2-9e98-f73d7ddfe20a
Signed-off-by: Pawel Beza <[email protected]>
Signed-off-by: Pawel Beza <[email protected]>
@pawelbeza
pawelbeza force-pushed the pawelbeza/combine-stdout-stderr branch from 90e8fda to c142335 Compare November 6, 2026 08:43
"exec",
"-cwd",
exec_cwd,
Non-append writes use tee when possible. The shell fallback creates

@pcmoritz pcmoritz Nov 6, 2026 •

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.

If you want to improve this going forward, we could ship a small statically linked binary to do this work as part of the Ray wheel (e.g. built via bazel, x86_64 and ARM depending on the wheel architecture), and then invoke it via runsc exec --exec-fd. This would both have the best performance in all cases, and would also have no dependence on any tools installed in the image.

@pcmoritz pcmoritz added sandbox go add ONLY when ready to merge, run all tests labels Nov 6, 2026
@pcmoritz
pcmoritz enabled auto-merge (squash) November 6, 2026 22:55
@pcmoritz
pcmoritz merged commit 014f687 into ray-project:master Nov 7, 2026
8 checks passed
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 9, 2026
ray-project#67876 added exec's combine_output, which leaves stderr as None and
returns the merged bytes in ExecResult.output. The OOM note now goes at
the end of output with combine_output, and into stderr without it.
ray-project#67688 runs every runsc exec through the launcher's wrap_command, so the
unit test's stand-in cgroup no longer passes through _wrap_workload.

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

community-contribution Contributed by the community 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