You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tighten AutoResume docs and trim use-case examples (#200)
* Tighten AutoResume docs and trim use-case examples
Rename title to "Auto resume", add a description, and restructure the page around a single web server example. Removes the agent/tool and per-user sandbox use cases.
* Move auto-resume note into code comments
* Address review comments on auto-resume docs
- Retitle to "Auto-resume on request"
- Replace "AutoResume" with "Auto-resume" throughout
- Convert lifecycle options list into a table
- Reframe option names as "onTimeout (JavaScript) / on_timeout (Python)"
- Qualify option references with the `lifecycle.` prefix
- Restore the standalone Cleanup section
* updated
---------
Co-authored-by: Mish Ushakov <10400064+mishushakov@users.noreply.github.com>
description: "Manage sandbox lifecycle and auto-resume on activity."
4
5
---
5
6
6
-
Many workloads don't need a sandbox running all the time, but when they do need it, it should just work, whether it was paused or not.
7
+
Many workloads don't need a sandbox running all the time, but when they do, it should just work — whether the sandbox was paused or not.
7
8
8
-
`AutoResume` handles this automatically: a paused sandbox wakes up when activity arrives, so your code does not have to check or manage sandbox state.
9
-
Configure it through the `lifecycle` object when creating a sandbox.
9
+
Auto-resume handles this automatically: a paused sandbox wakes up when activity arrives, so your code doesn't have to check or manage sandbox state. Auto-resume builds on the sandbox [persistence](/docs/sandbox/persistence) lifecycle.
10
10
11
-
## Configure lifecycle on create
11
+
## Configure
12
+
13
+
Set the `lifecycle` object when creating a sandbox to control what happens on timeout and whether paused sandboxes should auto-resume.
12
14
13
15
<CodeGroup>
14
16
```js JavaScript & TypeScript
@@ -35,24 +37,18 @@ sandbox = Sandbox.create(
35
37
```
36
38
</CodeGroup>
37
39
38
-
## Lifecycle options
39
-
40
-
-`onTimeout` / `on_timeout`
41
-
-`kill` (default): sandbox is terminated when timeout is reached
42
-
-`pause`: sandbox is paused when timeout is reached
43
-
-`autoResume` / `auto_resume`
44
-
-`false` (default): paused sandboxes do not auto-resume
45
-
-`true`: paused sandboxes auto-resume on activity
46
-
-`true` is valid only when `onTimeout`/`on_timeout` is `pause`
40
+
### Lifecycle options
47
41
48
-
## Behavior summary
42
+
The `lifecycle` object accepts the following options:
49
43
50
-
- Default behavior is equivalent to `onTimeout: "kill"` with `autoResume: false`.
51
-
-`onTimeout: "pause"` with `autoResume: false` gives auto-pause without auto-resume.
52
-
-`onTimeout: "pause"` with `autoResume: true` gives auto-pause with auto-resume.
53
-
-[`Sandbox.connect()`](/docs/sandbox/connect) can still be used to resume a paused sandbox manually.
44
+
| Setting | Option | Description |
45
+
|---------|--------|-------------|
46
+
|`onTimeout` (JavaScript) / `on_timeout` (Python) |`"kill"`| (default) Sandbox is terminated when timeout is reached |
47
+
||`"pause"`| Sandbox is paused when timeout is reached |
48
+
|`autoResume` (JavaScript) / `auto_resume` (Python) |`false`| (default) Paused sandboxes do not auto-resume |
49
+
||`true`| Paused sandboxes auto-resume on activity. Valid only when `onTimeout` / `on_timeout` is set to `"pause"`|
54
50
55
-
If you use `autoResume: false`, resume explicitly with [`Sandbox.connect()`](/docs/sandbox/connect).
51
+
If `lifecycle.autoResume` / `lifecycle.auto_resume` is falsy or unset, you can still resume a paused sandbox manually with [`Sandbox.connect()`](/docs/sandbox/connect).
56
52
57
53
## Timeout after auto-resume
58
54
@@ -68,26 +64,28 @@ For example, if you create a sandbox with a 2-minute timeout:
68
64
69
65
If you create a sandbox with a 1-hour timeout, the auto-resume timeout will also be 1 hour, since it exceeds the 5-minute minimum.
70
66
71
-
This cycle repeats every time the sandbox auto-resumes — the lifecycle configuration (`onTimeout: "pause"` + `autoResume: true`) is persistent across pause/resume cycles.
67
+
This cycle repeats every time the sandbox auto-resumes — the lifecycle configuration is persistent across pause/resume cycles.
72
68
73
69
<Note>
74
-
You can change the timeout after the sandbox resumes by calling `setTimeout`/`set_timeout`. See [Change sandbox timeout during runtime](/docs/sandbox#change-sandbox-timeout-during-runtime).
70
+
You can change the timeout after the sandbox resumes by calling `setTimeout()` (JavaScript) / `set_timeout()` (Python). See [Change sandbox timeout during runtime](/docs/sandbox#change-sandbox-timeout-during-runtime).
75
71
</Note>
76
72
77
73
## What counts as activity
78
74
79
-
Auto-resume is triggered by the sandbox activity - that's both HTTP traffic and controlling the sandbox from the SDK.
75
+
Auto-resume is triggered by sandbox activity — both HTTP traffic and SDK operations.
80
76
81
-
That includes SDK operations like:
77
+
That includes:
82
78
-`sandbox.commands.run(...)`
83
79
-`sandbox.files.read(...)`
84
80
-`sandbox.files.write(...)`
85
-
-opening a tunneled app URL or sending requests to a service running inside the sandbox
81
+
-Opening a tunneled app URL or sending requests to a service running inside the sandbox
86
82
87
-
If a sandbox is paused and `autoResume` is enabled, the next supported operation resumes it automatically. You do not need to call [`Sandbox.connect()`](/docs/sandbox/connect) first.
83
+
If a sandbox is paused and `lifecycle.autoResume` (JavaScript) / `lifecycle.auto_resume` (Python) is enabled, the next supported operation resumes it automatically. You do not need to call [`Sandbox.connect()`](/docs/sandbox/connect) first.
88
84
89
85
### SDK example: pause, then read a file
90
86
87
+
The following example writes a file, pauses the sandbox, then reads the file back. The read operation triggers an automatic resume.
88
+
91
89
<CodeGroup>
92
90
```js JavaScript & TypeScript
93
91
import { Sandbox } from'e2b'
@@ -127,108 +125,16 @@ print(f"State after read: {sandbox.get_info().state}")
127
125
```
128
126
</CodeGroup>
129
127
130
-
## Use cases
131
-
132
-
### Web and dev/preview servers
133
-
134
-
Use `onTimeout: "pause"` + `autoResume: true` so inbound traffic can wake a paused sandbox automatically.
Auto-resume is especially useful for web servers and preview environments. When an HTTP request arrives at a paused sandbox, the sandbox wakes up automatically to handle it.
211
131
212
-
time.sleep(1)
213
-
214
-
preview_host = sandbox.get_host(3000)
215
-
print(f"Preview URL: https://{preview_host}")
216
-
217
-
print(f"Status before pause: {sandbox.get_info().state}")
218
-
sandbox.pause()
219
-
print(f"Status after pause: {sandbox.get_info().state}")
220
-
```
221
-
</CodeGroup>
222
-
223
-
### Agent/tool execution
224
-
225
-
For queued tasks or tool calls, create once and keep using the same sandbox handle. If it is paused, it will auto-resume when you run the next command.
132
+
The following example starts a simple HTTP server and retrieves its public URL. Use [`getHost()`](/docs/sandbox/internet-access#sandbox-public-url) (JavaScript) / [`get_host()`](/docs/sandbox/internet-access#sandbox-public-url) (Python) to get the sandbox's publicly accessible hostname for a given port.
For multi-tenant apps, keep a map of sandbox IDs by user. On each request, connect to the user's existing sandbox (which auto-resumes if paused) or create a new one.
272
-
273
-
<CodeGroup>
274
-
```js JavaScript & TypeScript
275
-
import { Sandbox } from'e2b'
276
-
277
-
constuserSandboxes=newMap() // userId → Sandbox
278
-
279
-
asyncfunctiongetSandbox(userId) {
280
-
let sandbox =userSandboxes.get(userId)
281
-
282
-
if (!sandbox) {
283
-
sandbox =awaitSandbox.create({
284
-
timeoutMs:5*60*1000,
285
-
lifecycle: {
286
-
onTimeout:'pause',
287
-
autoResume:true,
288
-
},
289
-
})
290
-
userSandboxes.set(userId, sandbox)
291
-
}
292
-
293
-
return sandbox
294
-
}
295
-
296
-
// On each user request (auto-resumes if paused)
297
-
constsandbox=awaitgetSandbox('user-123')
298
-
constresult=awaitsandbox.commands.run('echo "Hello from your sandbox"')
result = sandbox.commands.run('echo "Hello from your sandbox"')
321
-
print(result.stdout)
165
+
host = sandbox.get_host(3000)
166
+
# Once the sandbox times out and pauses, any request to the preview URL will automatically resume it.
167
+
print(f"Preview URL: https://{host}")
322
168
```
323
169
</CodeGroup>
324
170
325
171
## Cleanup
326
-
Auto-resume is persistent, meaning if your sandbox resumes and later times out again, it will pause again.
172
+
173
+
Auto-resume is persistent — if your sandbox resumes and later times out again, it will pause again.
327
174
Each time the sandbox resumes, it gets a fresh timeout (at least 5 minutes, or longer if the original creation timeout exceeds that) — so the sandbox keeps cycling between running and paused as long as activity arrives.
175
+
328
176
If you call `.kill()`, the sandbox is permanently deleted and cannot be resumed.
0 commit comments