Sandbox. See Commands for usage examples.
Typical flow
Run and collect
sandbox.exec()
Example
Example
exec_stream() instead. Raises ExecTimeoutError if timeout elapses and ExecFailedError if the process can’t be spawned.
Parameters
cmdstrCommand to execute (e.g.
“python3”, “/usr/bin/node”).argslist[str] | Mapping[str, Any] | NoneCommand arguments. A mapping is accepted as a shorthand for the keyword-only options below.
cwdstr | NoneWorking directory for this command.
userstr | NoneGuest user to run as.
envMapping[str, str] | NoneEnvironment variables, merged on top of the sandbox defaults.
timeoutfloat | NoneSeconds before the process is killed.
Stdin mode. Raw
bytes / str are sent inline; default is /dev/null.ttyboolAllocate a pseudo-terminal, merging stdout and stderr.
rlimitslist[Rlimit] | NonePOSIX resource limits applied to the process.
Returns
Collected stdout, stderr, and exit status.
sandbox.shell()
Example
Example
/bin/sh). Shell syntax like pipes, redirects, and && chains works. Accepts the same keyword-only options as exec().
Parameters
scriptstrShell command string (e.g.
“ls -la /app && echo done”).cwd, user, env, timeout, stdin, tty, rlimitsSame per-call options as exec().
Returns
Collected stdout, stderr, and exit status.
Stream
sandbox.exec_stream()
Example
Example
ExecHandle that emits stdout, stderr, and exit events as they happen rather than buffering everything. Takes the same per-call options as exec(). Pass stdin=Stdin.pipe() to write to the process while it runs via take_stdin().
Parameters
cmdstrCommand to execute.
argslist[str] | Mapping[str, Any] | NoneCommand arguments.
cwd, user, env, timeout, stdin, tty, rlimitsSame per-call options as exec().
Returns
Streaming handle for receiving events and controlling the process.
sandbox.shell_stream()
Example
Example
shell(): runs script through the configured shell but returns an ExecHandle instead of buffering output.
Parameters
scriptstrShell command string.
cwd, user, env, timeout, stdin, tty, rlimitsSame per-call options as exec().
Returns
Streaming handle.
Attach
sandbox.attach()
Example
Example
Ctrl+]) to disconnect without stopping the process. Returns the process exit code.
Parameters
cmdstrCommand to run.
argslist[str] | NoneCommand arguments.
cwdstr | NoneWorking directory.
userstr | NoneGuest user to run as.
envMapping[str, str] | NoneEnvironment variables for the session.
detach_keysstr | NoneDetach key sequence (e.g.
“ctrl-]” or “ctrl-p,ctrl-q”).Returns
int
Exit code of the process.
sandbox.attach_shell()
Example
Example
Returns
int
Exit code of the shell process.
Types
ExecOutput
Returned by exec() · shell() · ExecHandle.collect()
The result of a completed command execution: collected output plus exit status. All members are properties.| Property | Type | Description |
|---|---|---|
| exit_code | int | Process exit code |
| success | bool | True when exit_code == 0 |
| stdout_text | str | Collected stdout decoded as UTF-8. Raises on invalid encoding |
| stderr_text | str | Collected stderr decoded as UTF-8. Raises on invalid encoding |
| stdout_bytes | bytes | Raw stdout bytes |
| stderr_bytes | bytes | Raw stderr bytes |
ExecHandle
Returned by exec_stream() · shell_stream()
A handle to a running streaming execution. It is an async iterator, soasync for event in handle: yields each ExecEvent until the stream ends.
| Property / Method | Type | Description |
|---|---|---|
| id | str | Correlation ID for this execution |
| take_stdin() | ExecSink | None | Take the stdin writer. Returns None after the first call, or when stdin wasn’t piped |
| recv() | ExecEvent | None | (async) Receive the next event. Returns None when the stream ends |
| wait() | tuple[int, bool] | (async) Wait for the process to exit. Returns (code, success) |
| collect() | ExecOutput | (async) Drain remaining output and wait for exit |
| signal(sig) | None | (async) Send a POSIX signal (numeric) to the process |
| kill() | None | (async) Send SIGKILL to the process |
ExecSink
Returned by ExecHandle.take_stdin()
Writer for sending data to a running process’s stdin. Obtained fromExecHandle.take_stdin() when the execution was configured with stdin=Stdin.pipe().
| Method | Parameters | Description |
|---|---|---|
| write(data) | data: bytes | (async) Write bytes to the process’s stdin |
| close() | - | (async) Close stdin. The process sees EOF |
ExecEvent
Emitted by ExecHandle
Native event object emitted byrecv() and by iterating an ExecHandle. Fields that don’t apply to a given event are None.
| Property | Type | Description |
|---|---|---|
| event_type | str | "started", "stdout", "stderr", "exited", "failed", or "stdin_error" |
| pid | int | None | Guest PID, set on "started" |
| data | bytes | None | Output bytes on "stdout" / "stderr", or a UTF-8 failure message on "failed" / "stdin_error" |
| code | int | None | Exit code on "exited", or errno when available on "failed" / "stdin_error" |
ExitStatus
Used by ExecHandle.wait()
Frozen dataclass describing a process exit result.ExecHandle.wait() returns the same information as a (code, success) tuple.
| Field | Type | Description |
|---|---|---|
| code | int | Process exit code |
| success | bool | True when code == 0 |
Stdin
Used by exec() · shell() · exec_stream() · shell_stream()
Frozen dataclass with factory methods for configuring process stdin. Pass the result as thestdin argument to an exec or shell method. Raw bytes and str are also accepted directly and are sent inline.
| Factory | Parameters | Description |
|---|---|---|
| Stdin.null() | - | Connect stdin to /dev/null (default) |
| Stdin.pipe() | - | Open a writable pipe. Write via take_stdin() on the handle |
| Stdin.bytes(data) | data: bytes | Inline data sent before the process starts, then EOF |
Rlimit
Used by exec() · shell() · exec_stream() · shell_stream()
Frozen dataclass describing a POSIX resource limit. Construct one directly or via a factory, then pass a list as therlimits argument.
| Factory | Parameters | Description |
|---|---|---|
| Rlimit.nofile(limit) | limit: int | Max open file descriptors |
| Rlimit.cpu(secs) | secs: int | CPU time limit in seconds |
| Rlimit.as_(*, soft, hard) | soft: int, hard: int | Virtual memory size |
| Rlimit.nproc(limit) | limit: int | Max number of processes |
| Rlimit.fsize(limit) | limit: int | Max file size |
| Rlimit.memlock(limit) | limit: int | Max locked memory |
| Rlimit.stack(limit) | limit: int | Max stack size |
| Field | Type | Description |
|---|---|---|
| resource | RlimitResource | Which resource is limited |
| soft | int | Soft limit |
| hard | int | Hard limit |
RlimitResource
Used by Rlimit.resource
String enum (enum.StrEnum) naming a limitable POSIX resource.
| Value | Description |
|---|---|
CPU | CPU time |
FSIZE | File size |
DATA | Data segment size |
STACK | Stack size |
CORE | Core file size |
RSS | Resident set size |
NPROC | Number of processes |
NOFILE | Open file descriptors |
MEMLOCK | Locked memory |
AS | Virtual memory |
LOCKS | File locks |
SIGPENDING | Pending signals |
MSGQUEUE | Message queue size |
NICE | Nice priority ceiling |
RTPRIO | Real-time priority ceiling |
RTTIME | Real-time CPU time |