Repository navigation
Description
Summary
Starting a session that has a restorable NATIVE_SNAPSHOT creates the sandbox twice — first from the original template, then from the snapshot — and kills the first one. The first sandbox's result is thrown away, so a restore costs 4 platform calls and 2 sandbox cold starts instead of 1.
Where
E2bSandbox.start() (E2bSandbox.java:68) runs ensureSandbox() before super.start():
-
ensureSandbox()(:228) — whengetSandboxId()is blank, or whenconnectSandboxfails, it creates frome2bState.getTemplateId()(:232-233,:244-245).Create Prepare for first release #1 — original template.
-
super.start()→AbstractBaseSandbox.start()Branch B/C →doHydrateWorkspace()(:183) reads the native snapshot id out of the persisted archive and callsrestoreSandboxFromSnapshotTemplate(nativeId)(:258) → creates fromsnapshotTemplateId(:261).Create Demo failed #2 — snapshot. Then
killSandbox(oldId)deletes create Prepare for first release #1.
So the sequence on every snapshot restore is:
POST /sandboxes templateID=<template> ← wasted
POST /sandboxes templateID=<snapshotID>
DELETE /sandboxes/<first>
Why it always happens
#3390 reports that SandboxManager.release always calls shutdown(), which deletes the E2B sandbox. The connectSandbox path in ensureSandbox therefore never succeeds, so every session takes the create branch and pays the double create. (E2bSandboxRecoveryTest.recreateClearsStaleWorkspaceProjectionState already pins the connect-fails-then-create path.)
Secondary effect: a leaked sandbox on failure
In restoreSandboxFromSnapshotTemplate, oldId is captured (:259) and only killed after the create succeeds (:261 onward). If create #2 throws — for example the snapshot id is no longer valid, or the provider rejects the create — start() fails and create #1 is never deleted. A dead sandbox is left behind on every such failure.
Proposed fix
Decide the source before creating, instead of creating first and rebuilding second:
- Peek the native snapshot id out of the persisted archive (bounded read of the magic prefix + small JSON; a TAR archive must never be read whole). Return
nullon any failure so it degrades exactly like an unrestorable snapshot already does. - If an id is present, create from it directly — the restore is then one create.
- Remember which snapshot the live sandbox was created from, and short-circuit
restoreSandboxFromSnapshotTemplatewhen it matches, sodoHydrateWorkspacedoes not rebuild whatensureSandboxalready built. - If the snapshot create fails, fall back to the template create, so a bad snapshot degrades to a fresh sandbox instead of failing
start()— this also removes the leak above.
peek only needs to run in NATIVE_SNAPSHOT mode, so TAR sessions keep the current cost.
Environment
- AgentScope Java
2.0.4-SNAPSHOT(upstreammainate68dd783) - Backend: E2B cloud (
NATIVE_SNAPSHOTpersistence) - Verified that
mainstill has this as ofe68dd783
Related
- [Feature]: Option to retain (or pause) sandboxes between calls instead of deleting them on every release #3390 — release deletes the sandbox, which is what makes the reconnect path dead and this double create universal
Activity
fix(e2b): restore NATIVE_SNAPSHOT sessions with a single sandbox (#3562)
fix(e2b): fail calls when snapshots cannot be restored (#3609)
fix(e2b): restore NATIVE_SNAPSHOT sessions with a single sandbox (age…
fix(e2b): fail calls when snapshots cannot be restored (agentscope-ai…