Skip to main content
Control the lifecycle of your sandboxes by starting, stopping, pausing, and resuming them. This allows you to manage resources efficiently and maintain state across sessions.

Prerequisites

Before you begin, make sure you have:
  • Existing sandbox - A sandbox that was created (see Creating Sandboxes)
  • Sandbox object - A Sandbox instance connected to your sandbox
  • Basic understanding - Familiarity with sandbox states and lifecycle

Sandbox States

Sandboxes can be in one of these states:
  • running - Active and ready to execute code
  • stopped - Shut down but can be restarted
  • paused - Temporarily suspended, can be resumed
  • creating - Currently being created
When you pause a sandbox, its state is preserved. When you resume it, everything continues from where it left off. Stopped sandboxes lose their runtime state but can be restarted.

Getting Current State

Check the current state of a sandbox:
Expected Output:

Starting a Sandbox

Start a stopped sandbox:
Expected Output:

Stopping a Sandbox

Stop a running sandbox (preserves filesystem but loses runtime state):
Expected Output:

Pausing a Sandbox

Pause a running sandbox (preserves full state including memory):
Expected Output:

Resuming a Sandbox

Resume a paused sandbox (restores full state):
Expected Output:

Complete State Management Example

Here’s a complete example showing all state operations:

State Transitions

Understanding state transitions helps you manage sandboxes effectively:

Pause vs Stop

Pause: Preserves full state (memory, variables, running processes). Fast resume. Stop: Preserves filesystem only. Runtime state is lost. Requires full restart.

When to Use

Pause: When you need to temporarily suspend work but keep everything in memory. Stop: When you want to free resources but keep files for later.

Error Handling

Handle state transition errors:

Best Practices

1

1. Use Pause for Temporary Suspension

Use pause() when you need to temporarily suspend work but want to preserve all state. Resume is fast and preserves everything.
2

2. Use Stop for Resource Management

Use stop() when you want to free resources but keep the filesystem. Files are preserved, but runtime state is lost.
3

3. Check Status Before Operations

Always check the sandbox status before performing operations to avoid errors.
4

4. Handle State Transitions Gracefully

Handle errors when transitioning states - a sandbox might already be in the desired state.
5

5. Clean Up When Done

Always call kill() when you’re completely done with a sandbox to free resources.

Implementation

Next Steps