os/exec conventions: a non-zero exit code is not a Go error. Transport, timeout, and spawn-failure paths return an error; a program that ran and exited non-zero is a normal *ExecOutput result, inspect Success() or ExitCode().
Typical flow
Sandbox methods
The exec entry points live on*Sandbox.
sb.Exec()
Example
Example
ExecOutput.ExitCode, not as an error.
Parameters
ctxcontext.Contextcmdstringargs[]stringnil.opts…ExecOptionReturns
sb.Shell()
Example
Example
/bin/sh -c command in the sandbox and collect its output. Blocks until the command exits. A convenience wrapper over Exec for shell one-liners.
Parameters
ctxcontext.Contextcommandstring/bin/sh -c.opts…ExecOptionReturns
sb.ExecStream()
Example
Example
*ExecHandle. The handle MUST be closed with Close when the stream is no longer needed. Nonblocking: the handle returns immediately and the stream starts in the background.
ctx controls only the start handshake; individual Recv calls take their own ctx. Non-zero exit codes are not errors, inspect ExecEventExited.
Parameters
ctxcontext.Contextcmdstringargs[]stringnil.opts…ExecOptionReturns
sb.ShellStream()
Example
Example
/bin/sh -c command with streaming output. A convenience wrapper over ExecStream. Nonblocking: the handle returns immediately and the stream starts in the background.
Parameters
ctxcontext.Contextcommandstring/bin/sh -c.opts…ExecOptionReturns
ExecHandle methods
A live streaming exec session returned byExecStream and ShellStream. Not safe for concurrent use from multiple goroutines.
h.ID()
Returns
h.TakeStdin()
Example
Example
nil if the session was not started with WithExecStdinPipe, or if TakeStdin was already called on this handle (matching the Node and Python SDKs). The caller is responsible for closing the sink when done writing; closing the sink without closing the exec handle is fine, they own different Rust-side resources.
Returns
nil if unavailable.h.Recv()
Example
Example
Kind == ExecEventDone when all events have been consumed. ctx controls the wait; cancellation causes Recv to return ctx.Err() immediately. The underlying Rust call may continue to completion in the background.
Parameters
ctxcontext.ContextReturns
h.Collect()
Example
Example
*ExecOutput. Equivalent to calling Recv in a loop and assembling the result. The handle should be closed after Collect returns.
Parameters
ctxcontext.ContextReturns
h.Wait()
Collect, stdout and stderr are discarded. The handle should be closed after Wait returns.
Parameters
ctxcontext.ContextReturns
h.Kill()
Parameters
ctxcontext.Contexth.Signal()
Example
Example
syscall (e.g. int(syscall.SIGTERM)).
Parameters
ctxcontext.Contextsignalintint(syscall.SIGTERM).h.Close()
Example
Example
Signal or Kill first if you need to terminate it. Safe to call after ExecEventDone has been received.
ExecOutput methods
The collected result of a completed command execution, returned byExec, Shell, and Collect.
out.Stdout()
Returns
out.StdoutBytes()
Returns
out.Stderr()
Returns
out.StderrBytes()
Returns
out.ExitCode()
-1 if the guest did not report one (e.g. the process was killed by a signal).
Returns
-1.out.Success()
0.
Returns
true if ExitCode() is 0.ExecSink methods
A write-only pipe to a running process’s stdin, obtained fromExecHandle.TakeStdin. Implements io.WriteCloser. Write and Close use context.Background() under the hood; for caller-controlled cancellation use WriteCtx, or tear the session down via ExecHandle.Kill / Close.
sink.Write()
Example
Example
io.Writer. Uses context.Background() internally, there is no way to cancel a stuck write through this method alone.
Parameters
p[]byteReturns
sink.WriteCtx()
Write, but with a caller-controlled context, so a stuck stdin write can be cancelled.
Parameters
ctxcontext.Contextp[]byteReturns
sink.Close()
Example
Example
io.Closer.
Types
ExecOutput
Returned by sb.Exec() · sb.Shell() · h.Collect()
The collected result of a command execution. A non-zeroExitCode is not treated as a Go error, callers inspect Success or ExitCode explicitly, matching how os/exec.Cmd.Output works against a script that exits non-zero.
| Method | Returns | Description |
|---|---|---|
Stdout() | string | Captured stdout as a string |
StdoutBytes() | []byte | Raw stdout bytes |
Stderr() | string | Captured stderr as a string |
StderrBytes() | []byte | Raw stderr bytes |
ExitCode() | int | Exit code, or -1 if the guest did not report one |
Success() | bool | true if ExitCode() is 0 |
ExecHandle
Returned by sb.ExecStream() · sb.ShellStream()
A live streaming exec session. Must be closed withClose when done to release Rust-side resources. Not safe for concurrent use from multiple goroutines.
| Method | Returns | Description |
|---|---|---|
ID() | (string, error) | Session correlation id assigned by the guest agent |
TakeStdin() | *ExecSink | Take the stdin writer (single-take; only with WithExecStdinPipe) |
Recv(ctx) | (*ExecEvent, error) | Block until the next event arrives |
Collect(ctx) | (*ExecOutput, error) | Drain remaining output into an ExecOutput |
Wait(ctx) | (int, error) | Wait for exit, discarding output; returns the exit code |
Kill(ctx) | error | Send SIGKILL |
Signal(ctx, signal) | error | Send a Unix signal |
Close() | error | Release the Rust-side handle |
ExecSink
Returned by h.TakeStdin()
io.WriteCloser.
| Method | Returns | Description |
|---|---|---|
Write(p) | (int, error) | Implements io.Writer |
WriteCtx(ctx, p) | (int, error) | Write with explicit context |
Close() | error | Send EOF and finalize |
ExecEvent
Returned by h.Recv()
One event from a streaming exec session.Kind identifies which fields are populated.
| Field | Type | Description |
|---|---|---|
Kind | ExecEventKind | Identifies which fields are populated |
PID | uint32 | Guest process id, set on ExecEventStarted |
Data | []byte | Chunk of stdout or stderr, set on ExecEventStdout / ExecEventStderr |
ExitCode | int | Process exit code, set on ExecEventExited |
Failure | *ExecFailure | Failure detail, set on ExecEventFailed and ExecEventStdinError |
ExecEventKind
Field of ExecEvent
ExecEvent carries.
| Constant | Description |
|---|---|
ExecEventStarted | Sent once when the guest process starts; PID is valid |
ExecEventStdout | A chunk of stdout; Data is valid |
ExecEventStderr | A chunk of stderr; Data is valid |
ExecEventExited | The process exited; ExitCode is valid |
ExecEventFailed | The user program never started (binary missing, permission denied, …); Failure is valid and ExitCode is not meaningful |
ExecEventStdinError | A stdin write failed; Failure is valid. Non-terminal: the process may still exit normally |
ExecEventDone | All events have been consumed |
ExecFailure
Field of ExecEvent
ExecEventFailed and ExecEventStdinError. See Error Handling for the kinds it carries (not_found, permission_denied, etc.) and how to branch on them.
| Field | Type | Description |
|---|---|---|
Kind | string | Failure category, e.g. not_found, permission_denied |
Errno | *int | Underlying errno, if known |
ErrnoName | string | Symbolic errno name, if known |
Message | string | Human-readable failure message |
Path | string | Offending path, if applicable |
ExecConfig
Populated by ExecOption
Configures a singleExec or ExecStream call. Most callers set fields through the WithExec* functional options; ExecConfig is exported for parity with the other SDKs’ config types.
| Field | Type | Description |
|---|---|---|
Cwd | string | Working directory inside the guest |
Timeout | time.Duration | Kill the process after this duration; sub-second precision is rounded up |
StdinPipe | bool | Enable a stdin pipe; required for TakeStdin |
User | string | Guest user (UID or name) |
Env | map[string]string | Per-command environment variables |
ExecOption
Accepted by sb.Exec() · sb.Shell() · sb.ExecStream() · sb.ShellStream()
ExecConfig. Construct them with the WithExec* functions below.
WithExecCwd()
Parameters
pathstringWithExecTimeout()
Example
Example
Kind == ErrExecTimeout. Sub-second precision rounds up to whole seconds; pass at least 1 second.
Parameters
dtime.DurationWithExecStdinPipe()
ExecHandle.TakeStdin.
WithExecUser()
Parameters
userstringWithExecEnv()
Example
Example
Parameters
envmap[string]string