Sandbox Exit Snapshots
Exit Snapshots take a filesystem snapshot automatically on both graceful and ungraceful Sandbox exits, including terminate(), entrypoint completion, idle timeout, lifetime expiry, and OOM.
This is useful when you want filesystem state after a Sandbox is gone without
having to call snapshot_filesystem() yourself first. Typical uses:
- Resuming an agent or coding session after an idle timeout
- Recovering work from a Sandbox that exited unexpectedly
- Avoiding races between your own teardown and an in-flight manual snapshot
Usage
Opt in at time of creation with experimental_options={"enable_exit_snapshot": True}, then fetch the resulting Image with _experimental_get_exit_snapshot() after the
Sandbox has exited:
import modal
app = modal.App.lookup("exit-snapshot-example", create_if_missing=True)
sb = modal.Sandbox.create(
"bash",
"-c",
"echo hello > /tmp/marker.txt",
app=app,
experimental_options={"enable_exit_snapshot": True},
)
sb.wait()
image = sb._experimental_get_exit_snapshot()
restored = modal.Sandbox.create(app=app, image=image)
print(restored.filesystem.read_text("/tmp/marker.txt")) # "hello\n"
restored.terminate()You can also reconnect later with Sandbox.from_id and fetch the exit
snapshot from that handle. This is useful when the create and resume happen
in different processes:
import modal
app = modal.App.lookup("exit-snapshot-example", create_if_missing=True)
# Process A: create a Sandbox, do work, then let it idle out or terminate.
sb = modal.Sandbox.create(
app=app,
experimental_options={"enable_exit_snapshot": True},
)
sb.filesystem.write_text("session state", "/workspace/state.txt")
sandbox_id = sb.object_id
sb.terminate(wait=True)
# Process B: resume from the exit snapshot.
sb = modal.Sandbox.from_id(sandbox_id)
image = sb._experimental_get_exit_snapshot()
sb2 = modal.Sandbox.create(app=app, image=image)
print(sb2.filesystem.read_text("/workspace/state.txt")) # "session state"
sb2.terminate()Waiting for the snapshot
_experimental_get_exit_snapshot(timeout=...) long-polls until the snapshot
reaches a terminal state:
timeout | Behavior |
|---|---|
None (default) | Wait until the snapshot succeeds or fails |
0 | Immediate check; raises TimeoutError if still pending |
> 0 | Wait up to the specified number of seconds |
On success, the method returns an Image you can pass to Sandbox.create(image=...).
Errors
_experimental_get_exit_snapshot() can raise these exceptions:
| Exception | When |
|---|---|
SnapshotCreationError | Snapshot creation failed |
TimeoutError | Still pending when your timeout elapses |
InvalidError | Exit snapshots were not enabled on the Sandbox, or timeout is negative |
NotFoundError | The Sandbox no longer exists |
These are all modal.exception classes. Note that TimeoutError is Modal’s own
class rather than Python’s builtin, so a bare except TimeoutError will not
catch it.
Retention
Exit snapshot Images are retained for 30 days after creation, matching the
default for filesystem snapshots.
After that, attempting to use the Image raises NotFoundError.
Limitations
- Filesystem only. Exit Snapshots capture the Sandbox filesystem, not memory or directory mounts.
- Main container only. Sidecar containers are not included.
- gVisor only. VM Sandboxes and Sandboxes v2 are not supported yet.
- Snapshot failure loses that Sandbox’s filesystem state. If you need to periodically persist state with stronger durability guarantees, prefer Volumes.