Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sandbox-timeout-instead-of-kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/cli': patch
---

Replace sandbox.kill() with sandbox.setTimeout(1s) in the CLI create command to prevent accidental historic sandbox snapshot deletions
10 changes: 6 additions & 4 deletions packages/cli/src/commands/sandbox/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ export async function connectSandbox({
sandbox: e2b.Sandbox
template: Pick<e2b.components['schemas']['Template'], 'templateID'>
}) {
// keep-alive loop
const intervalId = setInterval(async () => {
await sandbox.setTimeout(30_000)
// keep-alive loop — track the in-flight promise so we can await it on shutdown
let pendingKeepAlive: Promise<void> = Promise.resolve()
const intervalId = setInterval(() => {
pendingKeepAlive = sandbox.setTimeout(30_000)
}, 5_000)

console.log(
Expand All @@ -112,7 +113,8 @@ export async function connectSandbox({
await spawnConnectedTerminal(sandbox)
} finally {
clearInterval(intervalId)
Comment thread
matthewlouisbrockman marked this conversation as resolved.
await sandbox.kill()
await pendingKeepAlive.catch(() => {})
await sandbox.setTimeout(1_000)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prevent in-flight keepalive from overriding shutdown timeout

Using await sandbox.setTimeout(1_000) here can be undone by the async keep-alive interval above: clearInterval(intervalId) stops future ticks but does not stop a tick that already started, and that in-flight sandbox.setTimeout(30_000) can complete after this line and extend the sandbox lifetime back to 30s. This makes terminal-exit cleanup nondeterministic and can leave sandboxes running longer than intended; serialize/await pending keep-alive calls before applying the final 1s timeout.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed in 4612b72 — the interval callback no longer uses async/await and instead assigns the promise to a pendingKeepAlive variable. In the finally block we now await pendingKeepAlive.catch(() => {}) before setting the 1s shutdown timeout, ensuring any in-flight keep-alive completes first.

console.log(
`Closing terminal connection to template ${asFormattedSandboxTemplate(
template
Expand Down
Loading