agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: E2B NATIVE_SNAPSHOT restore creates two sandboxes and discards one

Open chcodex opened this issue 1 month ago • 0 comments

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():

  1. ensureSandbox() (:228) — when getSandboxId() is blank, or when connectSandbox fails, it creates from e2bState.getTemplateId() (:232-233, :244-245).

    Create #1 — original template.

  2. super.start() → AbstractBaseSandbox.start() Branch B/C → doHydrateWorkspace() (:183) reads the native snapshot id out of the persisted archive and calls restoreSandboxFromSnapshotTemplate(nativeId) (:258) → creates from snapshotTemplateId (:261).

    Create #2 — snapshot. Then killSandbox(oldId) deletes create #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:

  1. 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 null on any failure so it degrades exactly like an unrestorable snapshot already does.
  2. If an id is present, create from it directly — the restore is then one create.
  3. Remember which snapshot the live sandbox was created from, and short-circuit restoreSandboxFromSnapshotTemplate when it matches, so doHydrateWorkspace does not rebuild what ensureSandbox already built.
  4. 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 (upstream main at e68dd783)
  • Backend: E2B cloud (NATIVE_SNAPSHOT persistence)
  • Verified that main still has this as of e68dd783

Related

  • #3390 — release deletes the sandbox, which is what makes the reconnect path dead and this double create universal

chcodex avatar Oct 14 '26 09:10 chcodex