Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cloud-agent): wait before modal snapshot (#57818)

Alessandro Pogliaghi9781a1649055
diff --git a/products/tasks/backend/services/modal_sandbox.py b/products/tasks/backend/services/modal_sandbox.py
index 197cbcc25ee0..a07afd32a97a 100644
--- a/products/tasks/backend/services/modal_sandbox.py
+++ b/products/tasks/backend/services/modal_sandbox.py
@@ -700,6 +700,8 @@ class ModalSandbox(SandboxBase):
)
try:
+ # Modal can report the sandbox as running before filesystem snapshotting is ready.
+ self._sandbox.exec("true", timeout=30).wait()
image = self._sandbox.snapshot_filesystem()
snapshot_id = image.object_id
diff --git a/products/tasks/backend/services/tests/test_modal_sandbox.py b/products/tasks/backend/services/tests/test_modal_sandbox.py
index 4594cb4b6cfc..9bad62cbf72f 100644
--- a/products/tasks/backend/services/tests/test_modal_sandbox.py
+++ b/products/tasks/backend/services/tests/test_modal_sandbox.py
@@ -395,6 +395,28 @@ class TestModalSandboxAgentServer:
assert result is False
assert mock_sandbox.execute.call_count == 1
+ def test_create_snapshot_waits_for_container_before_snapshot(self, mock_sandbox: Any) -> None:
+ events: list[str] = []
+ exec_process = MagicMock()
+ exec_process.wait.side_effect = lambda: events.append("wait")
+ image = MagicMock()
+ image.object_id = "snapshot-123"
+
+ def snapshot_filesystem() -> Any:
+ events.append("snapshot")
+ return image
+
+ mock_sandbox._sandbox.exec.return_value = exec_process
+ mock_sandbox._sandbox.snapshot_filesystem.side_effect = snapshot_filesystem
+
+ result = mock_sandbox.create_snapshot()
+
+ assert result == "snapshot-123"
+ mock_sandbox._sandbox.exec.assert_called_once_with("true", timeout=30)
+ exec_process.wait.assert_called_once_with()
+ mock_sandbox._sandbox.snapshot_filesystem.assert_called_once_with()
+ assert events == ["wait", "snapshot"]
+
class TestModalSandboxProvisionDiagnostics:
@pytest.mark.parametrize(