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(data-imports): don't crash repartition detection on a persistent app-db blip (#80133)

Tom Owers668fc1184564
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py
index ce77a86e0f97..8b567e3d377f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py
@@ -12,6 +12,7 @@ import asyncio
from typing import TYPE_CHECKING, Any
from django.conf import settings
+from django.db import InterfaceError, OperationalError
from django.utils import timezone
import deltalake as deltalake
@@ -114,6 +115,13 @@ def _is_flag_enabled(schema: ExternalDataSchema, flag: str) -> bool:
team = retry_on_db_connection_drop(lambda: Team.objects.only("uuid", "organization_id").get(id=schema.team_id))
except Team.DoesNotExist:
return False
+ except (OperationalError, InterfaceError) as e:
+ # retry_on_db_connection_drop already retried once; a second failure is a genuinely degraded
+ # DB, not a bug here. Some callers (repartition_table.py) evaluate this flag with no enclosing
+ # try/except, so this function's contract of "never raises, defaults to disabled" must hold on
+ # its own.
+ capture_exception(e)
+ return False
try:
return bool(
posthoganalytics.feature_enabled(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
index 89e1edc2a85a..40670f4935c3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py
@@ -248,6 +248,23 @@ class TestIsAutoRepartitionEnabled:
assert mock_queryset.get.call_count == 2
+ def test_returns_false_when_db_connection_stays_down(self, team):
+ # If the connection is still down on the retry, is_auto_repartition_enabled must not raise —
+ # repartition_table.py calls it with no enclosing try/except, so an uncaught OperationalError
+ # here crashes the whole activity instead of just leaving the flag resolved as disabled.
+ schema = _make_schema(team, {})
+ mock_queryset = MagicMock()
+ mock_queryset.get.side_effect = OperationalError("server closed the connection unexpectedly")
+
+ with (
+ patch("posthog.models.Team.objects.only", return_value=mock_queryset),
+ patch.object(ctrl, "capture_exception") as mock_capture_exception,
+ ):
+ assert ctrl.is_auto_repartition_enabled(schema) is False
+
+ assert mock_queryset.get.call_count == 2
+ mock_capture_exception.assert_called_once()
+
class TestRepartitionOOMHistoryTrigger:
def _detect(self, team, schema: ExternalDataSchema, delta: deltalake.DeltaTable) -> None: