Skip to content

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

Closed

Description

@chcodex

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 Prepare for first release #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 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:

  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

Activity

  1. added a commit that references this issue on Oct 15, 2026
    e7bfda1
  2. added 2 commits that reference this issue on Oct 20, 2026
    195d04f
    ee77457
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions