: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(cohorts): propagate Celery soft time limit from cohort inserts (#63350)
Phil Haackafe32b95a0fd
diff --git a/posthog/test/test_cohort_model.py b/posthog/test/test_cohort_model.pyindex cb258227728f..305e5739f522 100644--- a/posthog/test/test_cohort_model.py+++ b/posthog/test/test_cohort_model.py@@ -3,9 +3,13 @@ import uuidimport pytestfrom posthog.test.base import BaseTest+from django.test import override_settings++from celery.exceptions import SoftTimeLimitExceededfrom parameterized import parameterizedfrom posthog.clickhouse.client import sync_execute+from posthog.helpers.batch_iterators import FunctionBatchIteratorfrom posthog.models import Person, Teamfrom products.cohorts.backend.models.cohort import Cohort@@ -397,6 +401,31 @@ class TestCohort(BaseTest):assert cohort.people.count() == 1assert str(cohort.people.first().uuid) == str(person.uuid)+ @override_settings(DEBUG=False)+ def test_insert_re_raises_soft_time_limit_exceeded(self):+ # A Celery soft-time-limit interruption must propagate so the task's time limit+ # bounds the run. The broad except must not swallow it (DEBUG=False forces the+ # production path where everything else is swallowed). The finally still finalizes+ # cohort state before it propagates, recording the timeout as a failed+ # calculation — not a successful one.+ cohort = Cohort.objects.create(team=self.team, groups=[], is_static=True)+ cohort.is_calculating = True+ cohort.save(update_fields=["is_calculating"])++ def _raise_timeout(batch_index: int, batch_size: int) -> list[str]:+ raise SoftTimeLimitExceeded()++ iterator = FunctionBatchIterator(_raise_timeout, batch_size=10, max_items=10)++ with self.assertRaises(SoftTimeLimitExceeded):+ cohort._insert_users_list_with_batching(iterator, team_id=self.team.id)++ cohort.refresh_from_db()+ self.assertFalse(cohort.is_calculating)+ self.assertEqual(cohort.errors_calculating, 1)+ self.assertIsNotNone(cohort.last_error_at)+ self.assertIsNone(cohort.last_calculation)+@parameterized.expand([# operator, filter_value, excluded_ages, included_agesdiff --git a/products/cohorts/backend/models/cohort.py b/products/cohorts/backend/models/cohort.pyindex 0d9a48921c52..f48e43b9424d 100644--- a/products/cohorts/backend/models/cohort.py+++ b/products/cohorts/backend/models/cohort.py@@ -14,6 +14,7 @@ from django.dispatch import receiverfrom django.utils import timezoneimport structlog+from celery.exceptions import SoftTimeLimitExceededfrom posthog.clickhouse.client import sync_executefrom posthog.clickhouse.query_tagging import Feature, tag_queries@@ -629,6 +630,14 @@ class Cohort(FileSystemSyncMixin, RootTeamMixin, models.Model):"""cursor.execute(query, params)+ except SoftTimeLimitExceeded as err:+ # Let a Celery soft-time-limit interruption propagate so the task's time limit+ # actually bounds the run. Swallowing it here (as the broad except below would)+ # leaves the caller's loop running past the limit, since Celery raises it once.+ # Record it as a processing error so the finally marks the run as failed+ # rather than a successful calculation.+ processing_error = err+ raiseexcept Exception as err:processing_error = err# When the caller owns terminal-state finalization (raise_on_error), surface