: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(data-imports): retry a dropped connection before reporting the fanout reuse flag check (#88342)
Tom Owersdffcda8780b9
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/fanout_reuse_flag.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/fanout_reuse_flag.pyindex cf5ef7ce3b49..3fb81169d35c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/fanout_reuse_flag.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/fanout_reuse_flag.py@@ -1,7 +1,10 @@+import django.db+import posthoganalyticsfrom posthog.exceptions_capture import capture_exceptionfrom posthog.models.team.team import Team+from posthog.temporal.common.utils import retry_on_db_connection_dropFANOUT_WAREHOUSE_REUSE_FLAG = "warehouse-fanout-parent-reuse"@@ -14,9 +17,14 @@ def is_fanout_warehouse_reuse_enabled(team_id: int) -> bool:the Delta reader stack into their import graph."""try:- team = Team.objects.only("uuid", "organization_id").get(id=team_id)+ team = retry_on_db_connection_drop(lambda: Team.objects.only("uuid", "organization_id").get(id=team_id))except Team.DoesNotExist:return False+ except (django.db.OperationalError, django.db.InterfaceError) as e:+ # retry_on_db_connection_drop already retried once; a second failure is a genuinely+ # degraded DB, not a bug here (see repartition_controller.py's _is_flag_enabled).+ capture_exception(e)+ return Falseexcept Exception as e:capture_exception(e)return Falsediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_fanout_reuse_flag.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_fanout_reuse_flag.pyindex a40146d44a68..1d8b4e7a2b3a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_fanout_reuse_flag.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_fanout_reuse_flag.py@@ -1,5 +1,9 @@import pytest-from unittest.mock import patch+from unittest.mock import MagicMock, patch++import django.db++from posthog.models.team.team import Teamfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.fanout_reuse_flag import (is_fanout_warehouse_reuse_enabled,@@ -26,3 +30,43 @@ class TestFanoutReuseFlagFailsClosed:@pytest.mark.django_dbdef test_missing_team_returns_false(self):assert is_fanout_warehouse_reuse_enabled(999999999) is False++ def test_transient_db_connection_drop_retries_then_succeeds(self):+ # A pooled connection dropped once (pgbouncer recycle) must not be reported as an+ # exception nor fail the gate closed — the retry should recover it transparently.+ mock_team = MagicMock()+ with (+ patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.fanout_reuse_flag.Team"+ ) as team_cls,+ patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.fanout_reuse_flag.posthoganalytics.feature_enabled",+ return_value=True,+ ),+ patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.fanout_reuse_flag.capture_exception"+ ) as mock_capture,+ ):+ team_cls.DoesNotExist = Team.DoesNotExist+ team_cls.objects.only.return_value.get.side_effect = [+ django.db.OperationalError("connection closed"),+ mock_team,+ ]+ assert is_fanout_warehouse_reuse_enabled(1) is True+ mock_capture.assert_not_called()++ def test_persistent_db_connection_drop_returns_false(self):+ # A second failure after the retry means a genuinely degraded DB, not a transient+ # blip — this should still be reported and the gate should still fail closed.+ with (+ patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.fanout_reuse_flag.Team"+ ) as team_cls,+ patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.fanout_reuse_flag.capture_exception"+ ) as mock_capture,+ ):+ team_cls.DoesNotExist = Team.DoesNotExist+ team_cls.objects.only.return_value.get.side_effect = django.db.OperationalError("connection closed")+ assert is_fanout_warehouse_reuse_enabled(1) is False+ mock_capture.assert_called_once()