Skip to content

Commit a451434

Browse files
committed
Clarify persistence information
1 parent aa106ee commit a451434

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

src/content/docs/sandbox/concepts/architecture.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class Sandbox extends DurableObject<Env> {
6868
**Why Durable Objects**:
6969

7070
- **Persistent identity** - Same sandbox ID always routes to same instance
71-
- **State management** - Durable Object persists state across ephemeral container restarts
71+
- **Container management** - Durable Object owns and manages the container lifecycle
7272
- **Geographic distribution** - Sandboxes run close to users
7373
- **Automatic scaling** - Cloudflare manages provisioning
7474

src/content/docs/sandbox/concepts/sandboxes.mdx

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A sandbox is an isolated execution environment where your code runs. Each sandbo
1010
- Has a unique identifier (sandbox ID)
1111
- Contains an isolated filesystem
1212
- Runs in a dedicated Linux container
13-
- Persists state between requests
13+
- Maintains state while the container is active
1414
- Exists as a Cloudflare Durable Object
1515

1616
## Lifecycle states
@@ -28,11 +28,11 @@ await sandbox.exec('echo "Hello"'); // First request creates sandbox
2828

2929
### Active
3030

31-
The sandbox is running and processing requests. Filesystem, processes, and environment variables persist across requests.
31+
The sandbox container is running and processing requests. All state remains available: files, running processes, shell sessions, and environment variables.
3232

3333
### Idle
3434

35-
After inactivity, the sandbox may enter idle state. Filesystem state is preserved, but the container may be paused. Next request triggers a warm start.
35+
After a period of inactivity, the container stops to free resources. When the next request arrives, a fresh container starts. All previous state is lost and the environment resets to its initial state.
3636

3737
### Destruction
3838

@@ -43,19 +43,23 @@ await sandbox.destroy();
4343
// All files, processes, and state deleted permanently
4444
```
4545

46-
## Persistence
46+
## Container lifetime and state
4747

48-
Between requests to the same sandbox:
48+
Sandbox state exists only while the container is active. Understanding this is critical for building reliable applications.
4949

50-
**What persists**:
51-
- Files in `/workspace`, `/tmp`, `/home`
52-
- Background processes (started with `startProcess()`)
53-
- Code interpreter contexts and variables
54-
- Environment variables and port exposures
50+
**While the container is active** (typically minutes to hours of activity):
51+
- Files written to `/workspace`, `/tmp`, `/home` remain available
52+
- Background processes continue running
53+
- Shell sessions maintain their working directory and environment
54+
- Code interpreter contexts retain variables and imports
5555

56-
**What doesn't persist**:
57-
- Nothing survives `destroy()`
58-
- Background processes may stop after container restarts (rare)
56+
**When the container stops** (due to inactivity or explicit destruction):
57+
- All files are deleted
58+
- All processes terminate
59+
- All shell state resets
60+
- All code interpreter contexts are cleared
61+
62+
The next request creates a fresh container with a clean environment.
5963

6064
## Naming strategies
6165

@@ -65,7 +69,7 @@ Between requests to the same sandbox:
6569
const sandbox = getSandbox(env.Sandbox, `user-${userId}`);
6670
```
6771

68-
User's work persists across sessions. Good for interactive environments, playgrounds, and notebooks.
72+
User's work persists while actively using the sandbox. Good for interactive environments, playgrounds, and notebooks where users work continuously.
6973

7074
### Per-session sandboxes
7175

@@ -111,18 +115,19 @@ try {
111115

112116
**Don't destroy**: Personal environments, long-running services
113117

114-
### Failure recovery
118+
### Handling container restarts
115119

116-
If container crashes or Durable Object is evicted (rare):
120+
Containers restart after inactivity or failures. Design your application to handle state loss:
117121

118122
```typescript
119-
try {
120-
await sandbox.exec('command');
121-
} catch (error) {
122-
if (error.message.includes('container') || error.message.includes('connection')) {
123-
await sandbox.exec('command'); // Retry - container recreates
124-
}
123+
// Check if required files exist before using them
124+
const files = await sandbox.listFiles('/workspace');
125+
if (!files.includes('data.json')) {
126+
// Reinitialize: container restarted and lost previous state
127+
await sandbox.writeFile('/workspace/data.json', initialData);
125128
}
129+
130+
await sandbox.exec('python process.py');
126131
```
127132

128133
## Best practices
@@ -131,7 +136,7 @@ try {
131136
- **Clean up temporary sandboxes** - Always destroy when done
132137
- **Reuse long-lived sandboxes** - One per user is often sufficient
133138
- **Batch operations** - Combine commands: `npm install && npm test && npm build`
134-
- **Handle failures** - Design for container restarts
139+
- **Design for ephemeral state** - Containers restart after inactivity, losing all state
135140

136141
## Related resources
137142

src/content/docs/sandbox/concepts/sessions.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Sessions are bash shell execution contexts within a sandbox. Think of them like
1212

1313
## Default session
1414

15-
Every sandbox has a default session that maintains shell state across commands:
15+
Every sandbox has a default session that maintains shell state between commands while the container is active:
1616

1717
```typescript
1818
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
@@ -25,7 +25,7 @@ await sandbox.exec("export MY_VAR=hello");
2525
await sandbox.exec("echo $MY_VAR"); // Output: hello
2626
```
2727

28-
Shell state persists: working directory, environment variables, exported variables all carry over between commands.
28+
Working directory, environment variables, and exported variables carry over between commands. This state resets if the container restarts due to inactivity.
2929

3030
## Creating sessions
3131

src/content/docs/sandbox/guides/code-execution.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ console.log('Success:', result.success);
7373
```
7474
</TypeScriptExample>
7575

76-
### Persistent state
76+
### State within a context
7777

78-
Variables and imports persist between executions in the same context:
78+
Variables and imports remain available between executions in the same context, as long as the container stays active:
7979

8080
<TypeScriptExample>
8181
```
@@ -102,6 +102,10 @@ console.log(result.output); // "Mean: 3.0"
102102
```
103103
</TypeScriptExample>
104104

105+
:::note
106+
Context state is lost if the container restarts due to inactivity. For critical data, store results outside the sandbox or design your code to reinitialize as needed.
107+
:::
108+
105109
## Handle rich outputs
106110

107111
The code interpreter returns multiple output formats:

0 commit comments

Comments
 (0)