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.

chore(tasks): deflake sandbox cleanup tests (#75762)

Raúl Negrón-Otero9ea48a360096
diff --git a/products/tasks/backend/temporal/conftest.py b/products/tasks/backend/temporal/conftest.py
index c91b4be99b9a..cae7d66e36d1 100644
--- a/products/tasks/backend/temporal/conftest.py
+++ b/products/tasks/backend/temporal/conftest.py
@@ -1,4 +1,5 @@
import os
+import time
import random
import pytest
@@ -28,6 +29,24 @@ def activity_environment():
return ActivityEnvironment()
+def assert_sandbox_shutdown():
+ """Assert a sandbox reaches SHUTDOWN, polling because `Sandbox.destroy()` does not wait.
+
+ `destroy()` fires Modal's terminate RPC and returns immediately, by design: the sandbox
+ TTL is the backstop if termination never lands. So the shutdown a cleanup activity causes
+ is only observable some time after the activity returns.
+ """
+
+ def _assert_sandbox_shutdown(sandbox_id: str, timeout_seconds: float = 60.0) -> None:
+ deadline = time.monotonic() + timeout_seconds
+ while Sandbox.get_by_id(sandbox_id).is_running():
+ assert time.monotonic() < deadline, f"sandbox {sandbox_id} still running after {timeout_seconds}s"
+ time.sleep(1)
+
+ return _assert_sandbox_shutdown
+
+
@pytest.fixture(autouse=True)
def posthog_code_oauth_app(request):
"""Create the Array OAuth application for DB-backed tests.
diff --git a/products/tasks/backend/temporal/create_snapshot/activities/tests/test_cleanup_sandbox.py b/products/tasks/backend/temporal/create_snapshot/activities/tests/test_cleanup_sandbox.py
index 41592802bb47..849f1ea7df81 100644
--- a/products/tasks/backend/temporal/create_snapshot/activities/tests/test_cleanup_sandbox.py
+++ b/products/tasks/backend/temporal/create_snapshot/activities/tests/test_cleanup_sandbox.py
@@ -3,7 +3,6 @@ import time
import pytest
-import modal
from asgiref.sync import async_to_sync
from products.tasks.backend.logic.services.sandbox import Sandbox, SandboxConfig, SandboxTemplate
@@ -19,7 +18,7 @@ from products.tasks.backend.temporal.create_snapshot.activities.cleanup_sandbox
)
class TestCleanupSandboxActivity:
@pytest.mark.django_db
- def test_cleanup_sandbox_success(self, activity_environment):
+ def test_cleanup_sandbox_success(self, activity_environment, assert_sandbox_shutdown):
test_tag = f"test-snapshot-cleanup-{time.time()}"
config = SandboxConfig(
name=f"test-snapshot-cleanup-sandbox-{time.time()}",
@@ -30,15 +29,13 @@ class TestCleanupSandboxActivity:
sandbox = Sandbox.create(config)
sandbox_id = sandbox.id
- sandboxes_before = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_before) > 0
+ assert Sandbox.get_by_id(sandbox_id).is_running()
input_data = CleanupSandboxInput(sandbox_id=sandbox_id)
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
- sandboxes_after = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_after) == 0
+ assert_sandbox_shutdown(sandbox_id)
@pytest.mark.django_db
def test_cleanup_sandbox_not_found_does_not_raise(self, activity_environment):
@@ -47,7 +44,7 @@ class TestCleanupSandboxActivity:
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
@pytest.mark.django_db
- def test_cleanup_sandbox_idempotency(self, activity_environment):
+ def test_cleanup_sandbox_idempotency(self, activity_environment, assert_sandbox_shutdown):
test_tag = f"test-snapshot-cleanup-idempotent-{time.time()}"
config = SandboxConfig(
name=f"test-snapshot-cleanup-idempotent-{time.time()}",
@@ -62,7 +59,6 @@ class TestCleanupSandboxActivity:
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
- sandboxes_after = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_after) == 0
+ assert_sandbox_shutdown(sandbox_id)
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_cleanup_sandbox.py b/products/tasks/backend/temporal/process_task/activities/tests/test_cleanup_sandbox.py
index b861b1948aec..c408edf5db50 100644
--- a/products/tasks/backend/temporal/process_task/activities/tests/test_cleanup_sandbox.py
+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_cleanup_sandbox.py
@@ -5,7 +5,6 @@ import threading
import pytest
-import modal
from asgiref.sync import async_to_sync
from products.tasks.backend.exceptions import SandboxNotFoundError
@@ -150,7 +149,7 @@ def test_cleanup_sandbox_completes_stream_when_sandbox_is_already_gone(activity_
)
class TestCleanupSandboxActivity:
@pytest.mark.django_db
- def test_cleanup_sandbox_success(self, activity_environment):
+ def test_cleanup_sandbox_success(self, activity_environment, assert_sandbox_shutdown):
test_tag = f"test-cleanup-{time.time()}"
config = SandboxConfig(
name=f"test-cleanup-sandbox-{time.time()}",
@@ -163,16 +162,13 @@ class TestCleanupSandboxActivity:
existing_sandbox = Sandbox.get_by_id(sandbox_id)
assert existing_sandbox.id == sandbox_id
-
- sandboxes_before = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_before) > 0
+ assert existing_sandbox.is_running()
input_data = CleanupSandboxInput(sandbox_id=sandbox_id)
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
- sandboxes_after = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_after) == 0
+ assert_sandbox_shutdown(sandbox_id)
@pytest.mark.django_db
def test_cleanup_sandbox_not_found_does_not_raise(self, activity_environment):
@@ -181,7 +177,7 @@ class TestCleanupSandboxActivity:
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
@pytest.mark.django_db
- def test_cleanup_sandbox_idempotency(self, activity_environment):
+ def test_cleanup_sandbox_idempotency(self, activity_environment, assert_sandbox_shutdown):
test_tag = f"test-cleanup-idempotent-{time.time()}"
config = SandboxConfig(
name=f"test-cleanup-idempotent-{time.time()}",
@@ -192,20 +188,18 @@ class TestCleanupSandboxActivity:
sandbox = Sandbox.create(config)
sandbox_id = sandbox.id
- sandboxes_before = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_before) > 0
+ assert Sandbox.get_by_id(sandbox_id).is_running()
input_data = CleanupSandboxInput(sandbox_id=sandbox_id)
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
- sandboxes_after = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_after) == 0
+ assert_sandbox_shutdown(sandbox_id)
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
@pytest.mark.django_db
- def test_cleanup_sandbox_during_execution(self, activity_environment):
+ def test_cleanup_sandbox_during_execution(self, activity_environment, assert_sandbox_shutdown):
test_tag = f"test-cleanup-during-exec-{time.time()}"
config = SandboxConfig(
name=f"test-cleanup-during-execution-{time.time()}",
@@ -227,13 +221,11 @@ class TestCleanupSandboxActivity:
time.sleep(5)
- sandboxes_before = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_before) > 0
+ assert Sandbox.get_by_id(sandbox_id).is_running()
input_data = CleanupSandboxInput(sandbox_id=sandbox_id)
async_to_sync(activity_environment.run)(cleanup_sandbox, input_data)
long_task.join(timeout=5)
- sandboxes_after = list(modal.Sandbox.list(tags={"test_tag": test_tag}))
- assert len(sandboxes_after) == 0
+ assert_sandbox_shutdown(sandbox_id)