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): silence closed-connection noise from stranded-run reconcile sweep (#90760)

Tom Owers4c0b91cff23d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py
index 95efaa70aa1c..c2482864508d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/consumer.py
@@ -443,6 +443,14 @@ class DeltaBatchConsumerAdapter:
# them on the same cadence and connection. Isolated so its failure can't take the sweep down.
try:
await self._reconcile_stale_stranded_runs(conn, stale_seconds=TAKEOVER_STALE_THRESHOLD_SECONDS, limit=limit)
+ except psycopg.OperationalError as e:
+ if conn.closed:
+ # A transient connection drop (network blip, server-side cull, pgbouncer bounce)
+ # leaves the connection closed. The engine reconnects on the next cycle.
+ logger.warning("stranded_run_reconcile_sweep_closed_connection", error=str(e))
+ else:
+ logger.exception("stranded_run_reconcile_sweep_failed")
+ capture_exception(e)
except Exception as e:
logger.exception("stranded_run_reconcile_sweep_failed")
capture_exception(e)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
index 3db4aec438e5..003fb93cf09d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py
@@ -1,6 +1,6 @@
import asyncio
from datetime import UTC, datetime
-from typing import Any
+from typing import Any, cast
import pytest
from unittest.mock import AsyncMock, patch
@@ -2021,6 +2021,53 @@ class TestReconcileFailedRuns:
mock_capture.assert_not_called()
+ @pytest.mark.parametrize(
+ "conn_closed,expect_capture",
+ [(True, False), (False, True)],
+ ids=["closed_conn_suppresses_capture", "open_conn_still_captured"],
+ )
+ @pytest.mark.asyncio
+ async def test_stranded_sweep_closed_connection_not_captured(self, conn_closed, expect_capture):
+ # A psycopg.OperationalError from a closed connection is a transient network
+ # drop — the engine reconnects on the next cycle. Only a real unexpected error
+ # (conn still open) should reach error tracking.
+ consumer = _make_consumer()
+ # consumer._recovery_conn starts healthy so _ensure_recovery_conn returns it
+ # without reconnecting. The side-effect below simulates the connection closing
+ # mid-sweep (as it does in practice when a network blip hits an active query).
+
+ async def raise_with_maybe_closed_conn(*args: object, **kwargs: object) -> None:
+ if conn_closed:
+ cast(Any, consumer._recovery_conn).closed = True
+ raise psycopg.OperationalError("the connection is closed")
+
+ with (
+ patch(
+ f"{consumer_module.__name__}.BatchQueue.get_oldest_unclaimed_batch_age_seconds",
+ new_callable=AsyncMock,
+ return_value=0.0,
+ ),
+ patch(
+ f"{consumer_module.__name__}.BatchQueue.get_claimable_batch_count",
+ new_callable=AsyncMock,
+ return_value=0,
+ ),
+ patch(
+ f"{consumer_module.__name__}.BatchQueue.get_failed_runs",
+ new_callable=AsyncMock,
+ return_value=[],
+ ),
+ patch(
+ f"{consumer_module.__name__}.BatchQueue.get_stale_stranded_runs",
+ new_callable=AsyncMock,
+ side_effect=raise_with_maybe_closed_conn,
+ ),
+ patch(f"{consumer_module.__name__}.capture_exception") as mock_capture,
+ ):
+ await consumer._reconcile_failed_runs()
+
+ assert mock_capture.called is expect_capture
+
class TestConnectionRecovery:
@pytest.mark.parametrize(