Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(server): keep an fsb snapshot whose create reply was lost

Maohao5e66f17ce52a
diff --git a/server/opensandbox_server/services/fast_sandbox/snapshot_runtime.py b/server/opensandbox_server/services/fast_sandbox/snapshot_runtime.py
index 8c3a85d77..10f51c7e2 100644
--- a/server/opensandbox_server/services/fast_sandbox/snapshot_runtime.py
+++ b/server/opensandbox_server/services/fast_sandbox/snapshot_runtime.py
@@ -169,11 +169,18 @@ class FastSandboxSnapshotRuntime:
reason=SNAPSHOT_CREATE_CONFLICT_REASON,
message=f"Failed to create fsb snapshot {snapshot_name}: {exc}",
)
- return SnapshotRuntimeStatus(
- state=SnapshotState.FAILED,
- reason="snapshot_runtime_create_failed",
- message=f"Failed to create fsb snapshot {snapshot_name}: {exc}",
- )
+ # The request is durable and idempotent by request_id, so it may
+ # have been persisted before the error (e.g. a lost reply). Report
+ # an existing snapshot, or it would be left without a record.
+ try:
+ existing = self._fastpath.get_sandbox_snapshot(ns, snapshot_name)
+ except FastPathError:
+ return SnapshotRuntimeStatus(
+ state=SnapshotState.FAILED,
+ reason="snapshot_runtime_create_failed",
+ message=f"Failed to create fsb snapshot {snapshot_name}: {exc}",
+ )
+ return self._status_from_info(existing.snapshot)
return SnapshotRuntimeStatus(
state=SnapshotState.CREATING,
reason="snapshot_runtime_submitted",
diff --git a/server/tests/test_fsb_snapshot_runtime.py b/server/tests/test_fsb_snapshot_runtime.py
index 562057c61..706aba1eb 100644
--- a/server/tests/test_fsb_snapshot_runtime.py
+++ b/server/tests/test_fsb_snapshot_runtime.py
@@ -181,6 +181,34 @@ def test_create_snapshot_maps_fastpath_errors_to_failed() -> None:
assert status.reason == "snapshot_runtime_create_failed"
+def test_create_snapshot_reports_snapshot_persisted_before_the_error() -> None:
+ # The request is durable and idempotent by request_id: when the reply is
+ # lost (e.g. the client deadline expires), the snapshot may already exist.
+ fastpath = FakeFastPathClient()
+ fastpath.create_error = FastPathError("DEADLINE_EXCEEDED", "deadline exceeded")
+ fastpath.get_by_name[("tenant-a", build_public_snapshot_name(SNAPSHOT_ID))] = _Response(
+ _Info(pb2.SNAPSHOT_PHASE_PUBLISHING)
+ )
+ runtime, _, _ = _runtime(fastpath=fastpath)
+
+ status = runtime.create_snapshot(SNAPSHOT_ID, SANDBOX_ID, namespace="tenant-a")
+
+ assert status.state == SnapshotState.CREATING
+ assert status.reason == "snapshot_runtime_in_progress"
+
+
+def test_create_snapshot_fails_when_no_snapshot_was_persisted() -> None:
+ fastpath = FakeFastPathClient()
+ fastpath.create_error = FastPathError("DEADLINE_EXCEEDED", "deadline exceeded")
+ runtime, _, _ = _runtime(fastpath=fastpath)
+
+ status = runtime.create_snapshot(SNAPSHOT_ID, SANDBOX_ID, namespace="tenant-a")
+
+ assert status.state == SnapshotState.FAILED
+ assert status.reason == "snapshot_runtime_create_failed"
+ assert "deadline exceeded" in (status.message or "")
+
+
def test_create_snapshot_maps_failed_precondition_to_conflict() -> None:
fastpath = FakeFastPathClient()
fastpath.create_error = FastPathError(