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(tasks): count warm pool capacity before locking the task (#104327)

Alessandro Pogliaghi619cdfd31aab
diff --git a/products/tasks/backend/logic/services/tests/test_warm.py b/products/tasks/backend/logic/services/tests/test_warm.py
index d0ed08d34688..fae1e35080d0 100644
--- a/products/tasks/backend/logic/services/tests/test_warm.py
+++ b/products/tasks/backend/logic/services/tests/test_warm.py
@@ -1,6 +1,7 @@
from posthog.test.base import APIBaseTest
from unittest.mock import patch
+from parameterized import parameterized
from rest_framework.exceptions import PermissionDenied, Throttled
from posthog.exceptions import QuotaLimitExceeded
@@ -56,9 +57,12 @@ class TestSandboxWarmerWarm(APIBaseTest):
assert kwargs["create_pr"] is False
assert kwargs["posthog_mcp_scopes"] == "full"
- def test_idempotent_when_non_terminal_run_exists(self):
+ @parameterized.expand([("empty_pool", 0), ("full_pool", _CAPS.per_user)])
+ def test_idempotent_when_non_terminal_run_exists(self, _name, other_warm_runs):
task = self._task()
existing = task.create_run(mode="interactive", extra_state={"await_user_message": True})
+ for _ in range(other_warm_runs):
+ self._warm_run_on_new_task()
with (
patch(f"{WARM}.execute_task_processing_workflow") as m_workflow,
diff --git a/products/tasks/backend/logic/services/warm.py b/products/tasks/backend/logic/services/warm.py
index ce9698f7aa33..2ae3cf89d64f 100644
--- a/products/tasks/backend/logic/services/warm.py
+++ b/products/tasks/backend/logic/services/warm.py
@@ -160,7 +160,10 @@ class SandboxWarmer:
(429) when the warm pool is full.
"""
# Quota is a gateway/cache call — check it before taking the row lock, never while holding it.
- self.enforce_quota(self.task.origin_product, self.task.team, self.user)
+ origin_product = self.task.origin_product
+ team = self.task.team
+ self.enforce_quota(origin_product, team, self.user)
+ pool_full = self.at_capacity(origin_product, team, self.user)
new_run: TaskRun
with transaction.atomic():
@@ -204,7 +207,7 @@ class SandboxWarmer:
if resume_source is None:
raise WarmSourceChanged
- if self.at_capacity(locked.origin_product, locked.team, self.user):
+ if pool_full:
raise Throttled(detail="Warm-pool capacity reached. Release an idle warm session and try again.")
run_state: dict[str, Any] = {