Skip to main content
Each sandbox runs as a child process of whatever application creates it. Sandbox.builder(...).create() boots a microVM, starts the guest agent inside it, and establishes a communication channel back to the host. Understanding the lifecycle is useful once you start managing long-running sandboxes, graceful shutdown, or resilient agent workflows.

States

Create a sandbox

Creating a sandbox boots the microVM, mounts the filesystem, initializes the guest agent, and waits until it’s ready to accept commands. Names must be non-empty and no longer than 128 UTF-8 bytes.

Converge on a named sandbox

Use connect_or_create when several callers may converge on the same reusable name. It connects if the current persisted sandbox is already running, starts it if it is stopped or crashed, and creates it only when the name is missing. If another caller wins a concurrent create or start, the operation observes the winner and converges on it.
Builder options apply only when a sandbox is created. An existing sandbox always keeps its persisted image, resources, environment, mounts, and other configuration. connect_or_create rejects replace options because replacing and converging express conflicting identity semantics. If you already hold a metadata handle, use connect_or_start instead. It connects without taking lifecycle ownership when the exact sandbox is running, waits through Starting, or starts the exact persisted sandbox when it is created, stopped, or crashed.
Draining is not reinterpreted as a start request, and Paused requires explicit resume support, which is not currently exposed.

Identity-safe receivers

Sandbox names are reusable lookup keys. Every Sandbox and SandboxHandle also exposes an opaque stable id (ID() in Go) for the persisted sandbox it represents. Do not parse this value; use it for logging, correlation, and equality checks. On the built-in local and cloud backends, receiver-based status and lifecycle operations remain bound to that captured identity. If worker is removed and another sandbox is created with the same name, a stale receiver cannot refresh, start, stop, kill, drain, restart, destroy, or remove the replacement. The SDK returns SandboxReplaced (or the language’s typed equivalent) when it can observe the new identity; ID-addressed cloud operations may instead report that the old resource is gone. Custom Rust backends should override the *_identified backend methods to provide the same guarantee. This closes the check-then-act race that an exists() helper would encourage:
Identity safety uses the existing local database row ID and cloud sandbox UUID. It requires no persistence migration or wire-protocol change.

Lifecycle invariants

The convergent APIs preserve the following contracts across all SDKs: The local coordination is per sandbox name. It does not introduce a global lifecycle lock, and it does not keep a database transaction open while an image is prepared or a VM boots:

Live-tested SDK surface

Every checked cell below is exercised by the in-tree lifecycle-convergence example against a real microVM. The examples also cover concurrent find/create and connect/start calls, existing-configuration precedence, detached start, force and timeout controls, exec after readiness, restart continuity, same-name recreation, and stale-receiver rejection.

Stop and restart

Stopping gracefully terminates guest processes and shuts down the VM. The sandbox moves to Stopped and can be restarted later with all its configuration preserved.
msb restart follows the same lifecycle semantics as msb stop followed by msb start. If the sandbox is already stopped or crashed, it starts it directly. SDK receivers also expose a convergent restart operation with graceful shutdown by default and options for force, timeout, and detached start where supported. On microsandbox cloud, restart and destroy can request graceful stop, but timeout expiry cannot escalate to force kill. Force controls are local-only, and detached-start controls affect only local process ownership.

Configuration

Use msb modify, or the SDK modify() methods, to change an existing sandbox without recreating it. Some changes apply live, some affect future execs only, and some need a restart or the next start.
See Tuning for the change model, CPU and memory resize, labels, environment variables, secrets, and storage sizing.

Ping and touch

Use ping to check that a running sandbox’s guest agent is reachable, and touch to intentionally refresh its idle timer. Ping is a health check only: it does not count as sandbox activity and will not keep an idle sandbox alive by itself. Touch is the explicit keepalive.

Kill immediately

If a sandbox is unresponsive (e.g., stuck in a tight loop or a panic), force-kill it. The sandbox is terminated immediately with no graceful shutdown.

Detach

Keeps a sandbox running after the parent process exits. It becomes a background process that you can reconnect to later with Sandbox::get("worker").

Request drain

Trigger a graceful shutdown that lets existing commands finish but rejects new ones. The sandbox moves to Draining and transitions to Stopped when all in-flight commands complete. This is useful for zero-downtime rotation of worker sandboxes.

Wait until stopped

Block until the sandbox is observed in a terminal non-running state, without triggering a stop or kill request.
Use wait_for_status / waitForStatus / WaitForStatus when you need any exact lifecycle state rather than only a terminal state. It intentionally has no built-in timeout; compose the language’s normal deadline or cancellation primitive around it.

Destroy

destroy combines stop and remove for the exact sandbox identity. It requests a graceful stop by default, escalates after the configured timeout, and refuses to act on a replacement that reused the name.

Remove

Delete a stopped sandbox. Every local SDK entry point and msb rm uses the same deletion scope.
For a local sandbox, removal deletes sandbox-owned state while leaving independently managed resources intact: Removing a sandbox does not undo writes made to a named volume, bind mount, or user-supplied disk image. On cloud, removal deletes the remote sandbox resource; the local disk details above do not apply.

List and inspect

Runtime process architecture

At runtime, your application talks to a host-side sandbox process, and that process relays requests to the guest agent inside the VM. The sandbox process also handles:
  • Graceful stop and drain signals
  • Cleanup when the sandbox exits
  • Idle detection and maximum lifetime enforcement

Logs and diagnostics

Use msb logs or the SDK logs() method to read captured output from running, stopped, or crashed sandboxes. For source semantics, boot errors, and diagnostic flows, see Logs.

Sandbox process policies

For production workloads, configure how the sandbox process handles shutdown, idle detection, and maximum lifetime.