docs/sdk/go/execution.mdx
Run commands inside a running sandbox, collect their output, stream events live, or drive an interactive pseudo-terminal programmatically. See Commands for usage examples.
The exec API mirrors 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().
import m "github.com/superradcompany/microsandbox/sdk/go"
out, err := sb.Exec(ctx, "python3", []string{"-c", "print(1 + 1)"})
if err != nil {
return err // transport / timeout / spawn failure
}
if !out.Success() {
log.Printf("exited %d: %s", out.ExitCode(), out.Stderr())
}
fmt.Print(out.Stdout())
The exec entry points live on *Sandbox.
func (s *Sandbox) Exec(ctx context.Context, cmd string, args []string, opts ...ExecOption) (*ExecOutput, error)
out, err := sb.Exec(ctx, "make", []string{"build"},
m.WithExecCwd("/app"),
m.WithExecEnv(map[string]string{"DEBUG": "1"}),
)
Run a command in the sandbox and return its collected output. Blocks until the command exits. The returned error is non-nil only on transport or runtime failures; a non-zero exit code is reported via ExecOutput.ExitCode, not as an error.
func (s *Sandbox) Shell(ctx context.Context, command string, opts ...ExecOption) (*ExecOutput, error)
out, err := sb.Shell(ctx, "echo $HOME && ls /tmp")
if err != nil {
return err
}
fmt.Print(out.Stdout())
Run /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.
func (s *Sandbox) ExecStream(ctx context.Context, cmd string, args []string, opts ...ExecOption) (*ExecHandle, error)
h, err := sb.ExecStream(ctx, "cat", nil, m.WithExecStdinPipe())
if err != nil {
return err
}
defer h.Close()
Start a streaming exec session and return an *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.
func (s *Sandbox) ShellStream(ctx context.Context, command string, opts ...ExecOption) (*ExecHandle, error)
h, err := sb.ShellStream(ctx, "tail -f /var/log/app.log")
if err != nil {
return err
}
defer h.Close()
for {
ev, err := h.Recv(ctx)
if err != nil {
return err
}
switch ev.Kind {
case m.ExecEventStdout:
os.Stdout.Write(ev.Data)
case m.ExecEventDone:
return nil
}
}
Run /bin/sh -c command with streaming output. A convenience wrapper over ExecStream. Nonblocking: the handle returns immediately and the stream starts in the background.
A live streaming exec session returned by ExecStream and ShellStream. Signal, Kill, and Resize may be called concurrently with Recv, allowing a control goroutine to act while another goroutine waits for terminal output. Other method combinations, including concurrent Recv calls, are not supported.
func (h *ExecHandle) ID() (string, error)
Return the unique identifier for this exec session, assigned by the guest agent. Useful for correlating log entries or referencing the session from external tooling.
<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">string</span></div> <div className="msb-param-desc">Session correlation id.</div> </div> </div>func (h *ExecHandle) TakeStdin() *ExecSink
sink := h.TakeStdin()
sink.Write([]byte("hello\n"))
sink.Close()
Return the stdin sink for this exec session. Single-take: returns 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.
func (h *ExecHandle) Recv(ctx context.Context) (*ExecEvent, error)
for {
ev, err := h.Recv(ctx)
if err != nil {
return err
}
switch ev.Kind {
case m.ExecEventStarted:
fmt.Printf("started pid=%d\n", ev.PID)
case m.ExecEventStdout:
os.Stdout.Write(ev.Data)
case m.ExecEventStderr:
os.Stderr.Write(ev.Data)
case m.ExecEventExited:
fmt.Printf("exited code=%d\n", ev.ExitCode)
case m.ExecEventDone:
return nil
}
}
Block until the next event arrives or the stream ends. Returns an event with 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.
func (h *ExecHandle) Collect(ctx context.Context) (*ExecOutput, error)
out, err := h.Collect(ctx)
if err != nil {
return err
}
fmt.Print(out.Stdout())
Drain the stream, accumulate all output, and return it as an *ExecOutput. Equivalent to calling Recv in a loop and assembling the result. The handle should be closed after Collect returns.
func (h *ExecHandle) Wait(ctx context.Context) (int, error)
Block until the process exits and return its exit code. Unlike Collect, stdout and stderr are discarded. The handle should be closed after Wait returns.
func (h *ExecHandle) Kill(ctx context.Context) error
Send SIGKILL to the running process.
<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the call.</div> </div> </div>func (h *ExecHandle) Signal(ctx context.Context, signal int) error
import "syscall"
if err := h.Signal(ctx, int(syscall.SIGTERM)); err != nil {
return err
}
Send a Unix signal to the running process. Pass values from syscall (e.g. int(syscall.SIGTERM)).
func (h *ExecHandle) Resize(ctx context.Context, rows, cols uint16) error
if err := h.Resize(ctx, 40, 120); err != nil {
return err
}
Resize the pseudo-terminal for this exec session. Use with a session started with WithExecTTY(true), typically when relaying a terminal-size change from a remote client.
Resize may be called concurrently while another goroutine is blocked in Recv.
func (h *ExecHandle) Close() error
defer h.Close()
Release the Rust-side exec handle. Does not kill the running process; call Signal or Kill first if you need to terminate it. Safe to call after ExecEventDone has been received.
The collected result of a completed command execution, returned by Exec, Shell, and Collect.
func (e *ExecOutput) Stdout() string
Captured standard output as a string.
<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">string</span></div> <div className="msb-param-desc">Collected stdout.</div> </div> </div>func (e *ExecOutput) StdoutBytes() []byte
Captured standard output as raw bytes. Use when the output may not be valid UTF-8.
<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">Raw stdout bytes.</div> </div> </div>func (e *ExecOutput) Stderr() string
Captured standard error as a string.
<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">string</span></div> <div className="msb-param-desc">Collected stderr.</div> </div> </div>func (e *ExecOutput) StderrBytes() []byte
Captured standard error as raw bytes.
<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">Raw stderr bytes.</div> </div> </div>func (e *ExecOutput) ExitCode() int
The process's exit code, or -1 if the guest did not report one (e.g. the process was killed by a signal).
func (e *ExecOutput) Success() bool
Reports whether the command exited with code 0.
A write-only pipe to a running process's stdin, obtained from ExecHandle.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.
func (sk *ExecSink) Write(p []byte) (int, error)
h, err := sb.ExecStream(ctx, "cat", nil, m.WithExecStdinPipe())
if err != nil {
return err
}
defer h.Close()
sink := h.TakeStdin()
sink.Write([]byte("hello\n"))
sink.Close()
out, _ := h.Collect(ctx)
fmt.Print(out.Stdout()) // "hello\n"
Send data to the process stdin. Implements io.Writer. Uses context.Background() internally, there is no way to cancel a stuck write through this method alone.
func (sk *ExecSink) WriteCtx(ctx context.Context, p []byte) (int, error)
Like Write, but with a caller-controlled context, so a stuck stdin write can be cancelled.
func (sk *ExecSink) Close() error
sink.Close()
Close the stdin sink. In non-TTY pipe mode this sends EOF to the process. In TTY mode it prevents further writes through this sink, but the guest PTY remains open; send the terminal's EOF control character (usually \x04 in canonical mode) when the interactive program needs one. Implements io.Closer.
The collected result of a command execution. A non-zero ExitCode 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. In TTY mode, Stdout contains the combined terminal output and Stderr is empty because a PTY cannot preserve separate stdout and stderr streams.
| 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 |
A live streaming exec session. Must be closed with Close when done to release Rust-side resources. Signal, Kill, and Resize may run concurrently with Recv; other method combinations, including multiple concurrent Recv calls, are not supported.
| 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 |
Resize(ctx, rows, cols) | error | Resize the pseudo-terminal |
Close() | error | Release the Rust-side handle |
type ExecSink = ffi.ExecSink
A write-only pipe to a running process's stdin. Implements io.WriteCloser.
| Method | Returns | Description |
|---|---|---|
Write(p) | (int, error) | Implements io.Writer |
WriteCtx(ctx, p) | (int, error) | Write with explicit context |
Close() | error | Close the sink; sends EOF in non-TTY pipe mode |
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 |
type ExecEventKind = ffi.ExecEventKind
Identifies what an ExecEvent carries.
| Constant | Description |
|---|---|
ExecEventStarted | Sent once when the guest process starts; PID is valid |
ExecEventStdout | A chunk of stdout; in TTY mode, carries the combined terminal output stream. Data is valid |
ExecEventStderr | A chunk of stderr; not emitted separately in TTY mode. 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 |
type ExecFailure = ffi.ExecFailure
Structured detail about a failed-to-start exec, populated on 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 |
Configures a single Exec 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 |
TTY | bool | Allocate a pseudo-terminal; default false |
User | string | Guest user (UID or name) |
Env | map[string]string | Per-command environment variables |
type ExecOption func(*ExecConfig)
A functional option that mutates an ExecConfig. Construct them with the WithExec* functions below.
func WithExecCwd(path string) ExecOption
Set the working directory for a single command.
<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>path</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Absolute path inside the guest.</div> </div> </div>func WithExecTimeout(d time.Duration) ExecOption
out, err := sb.Shell(ctx, "long-running-task",
m.WithExecTimeout(30*time.Second))
if m.IsKind(err, m.ErrExecTimeout) {
log.Println("timed out")
}
Set a per-command timeout. When exceeded, the guest terminates the process and the call returns an error with Kind == ErrExecTimeout. Sub-second precision rounds up to whole seconds; pass at least 1 second.
func WithExecStdinPipe() ExecOption
Enable a stdin pipe for the exec session, allowing data to be written via ExecHandle.TakeStdin.
func WithExecTTY(enabled bool) ExecOption
h, err := sb.ExecStream(ctx, "/bin/bash", nil,
m.WithExecStdinPipe(),
m.WithExecTTY(true),
)
if err != nil {
return err
}
defer h.Close()
stdin := h.TakeStdin()
if stdin == nil {
return errors.New("stdin pipe is unavailable")
}
if err := h.Resize(ctx, 40, 120); err != nil {
return err
}
if _, err := stdin.Write([]byte("echo hello\n")); err != nil {
return err
}
Control whether the command runs inside a pseudo-terminal. Enable it for interactive programs such as shells, editors, REPLs, and top. Default: false.
When enabled, the guest process runs with its stdin, stdout, and stderr connected to the PTY. Streaming sessions expose the combined terminal output through ExecEventStdout; they do not emit a separately attributable stderr stream. Collected output similarly places the combined stream in Stdout and leaves Stderr empty. Add WithExecStdinPipe() to write to the terminal programmatically, and use Resize to update its dimensions after the session starts.
func WithExecUser(user string) ExecOption
Run the command as the given guest user (UID or name).
<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>user</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Guest user, as a UID or name.</div> </div> </div>func WithExecEnv(env map[string]string) ExecOption
out, err := sb.Exec(ctx, "make", []string{"build"},
m.WithExecCwd("/app"),
m.WithExecEnv(map[string]string{"DEBUG": "1"}),
)
Add per-command environment variables. Called repeatedly, maps merge; later keys overwrite earlier ones.
<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>env</code><span className="msb-type">map[string]string</span></div> <div className="msb-param-desc">Environment variables for this command.</div> </div> </div>