: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): evict stale DB connection before recording CDC failure state (#74091)
Tom Owers36d293408457
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.pyindex ac42163bc861..546e94355944 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py@@ -1385,6 +1385,12 @@ class CDCExtractActivity:def _handle_failure(self, exc: Exception) -> CDCErrorInfo:"""Classify the failure, store the friendly message on the jobs/schemas, return the info."""self.log.exception("cdc_extract_failed")+ # `exc` itself may be a dropped/killed DB connection (e.g. the source read or a write+ # earlier in this run hit "server closed the connection unexpectedly"). Django leaves that+ # connection in the pool until the next query touches it, so without this the job/schema+ # writes below immediately re-fail with "the connection is closed", masking the real error+ # and leaving jobs stuck RUNNING instead of recording the friendly failure message.+ close_old_connections()info = classify_cdc_error(exc, self.adapter)friendly = info.friendly_message[:MAX_FRIENDLY_MESSAGE_LENGTH]self._fail_created_jobs(friendly)diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.pyindex 2da74dd8a080..d8a051c70d2d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py@@ -6,6 +6,8 @@ from typing import Literalimport pytestfrom unittest.mock import MagicMock, patch+from django.db.utils import OperationalError+import pyarrow as paimport psycopg.errorsfrom parameterized import parameterized@@ -968,6 +970,63 @@ class TestCDCExtractActivity:# Slot should NOT have been advancedmock_reader.confirm_position.assert_not_called()+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.activity")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.PostgresProducer")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.S3BatchWriter")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.get_cdc_adapter")+ @patch.object(CDCExtractActivity, "_get_cdc_schemas")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.ExternalDataSource")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.ExternalDataJob")+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.close_old_connections")+ def test_dropped_connection_during_flush_still_records_failure(+ self,+ mock_close_conns,+ MockJob,+ MockSourceModel,+ mock_get_schemas,+ mock_get_adapter,+ MockS3Writer,+ MockProducer,+ mock_activity,+ ):+ source = _make_source()+ schema = _make_schema("users", cdc_mode="streaming", source=source)+ events = [_make_event(op="I", table="users", position="0/100")]++ mock_reader, mock_s3, mock_producer, mock_job = _setup_mocks(+ mock_activity,+ MockProducer,+ MockS3Writer,+ mock_get_adapter,+ mock_get_schemas,+ MockSourceModel,+ MockJob,+ mock_close_conns,+ source,+ [schema],+ events,+ )++ # The source DB (or the warehouse-sources DB) drops the connection mid-flush — the same+ # OperationalError seen when Postgres kills a connection out from under a long-running+ # activity thread.+ mock_s3.write_batch.side_effect = OperationalError("the connection is closed")++ inputs = CDCExtractInput(team_id=1, source_id=source.id)++ with pytest.raises(OperationalError, match="the connection is closed"):+ cdc_extract_activity(inputs)++ # The stale connection left by the drop must be evicted again before the failure handler+ # writes the job/schema failure state, otherwise that write immediately re-raises the same+ # "connection is closed" error and the friendly message never gets recorded. One call from+ # run()'s own startup eviction, one from the failure handler.+ assert mock_close_conns.call_count == 2++ assert schema.status == "Failed"+ assert schema.latest_error == cdc_error_info(CDCErrorCategory.UNKNOWN).friendly_message+ mock_reader.confirm_position.assert_not_called()+@patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.activity")@patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.PostgresProducer")@patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.S3BatchWriter")