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(warehouse-sources): stop capturing transient queue connection drops (#100669)

Tom Owerse5fc02c479b7
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 56e0f31ee7fe..cb54c0ff256a 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
@@ -136,6 +136,16 @@ JOB_STATUS_CACHE_TTL_SECONDS = 30
JOB_STATUS_CACHE_MAX_ENTRIES = 1000
+def _is_transient_queue_connection_drop(err: Exception, conn: psycopg.AsyncConnection[Any]) -> bool:
+ """Whether `err` is a queue-db connection dying mid-query rather than a real bug.
+
+ A network blip, server-side cull, or pgbouncer bounce leaves `conn` closed; the next
+ reconcile cycle reconnects and retries, so it isn't worth paging on (mirrors the
+ conn.closed check already applied to the stranded-run sweep's own OperationalError).
+ """
+ return isinstance(err, psycopg.OperationalError) and conn.closed
+
+
class DeltaBatchConsumerAdapter:
log_prefix: str = ""
executing_state: str = SourceBatchStatus.State.EXECUTING.value
@@ -454,8 +464,11 @@ class DeltaBatchConsumerAdapter:
reason="enqueued into an already-failed run (reconcile sweep)",
)
except Exception as e:
- logger.exception("reconcile_straggler_sweep_failed", run_uuid=ref.run_uuid)
- capture_exception(e)
+ if _is_transient_queue_connection_drop(e, conn):
+ logger.warning("reconcile_straggler_sweep_closed_connection", run_uuid=ref.run_uuid, error=str(e))
+ else:
+ logger.exception("reconcile_straggler_sweep_failed", run_uuid=ref.run_uuid)
+ capture_exception(e)
return
if stragglers:
@@ -550,10 +563,13 @@ class DeltaBatchConsumerAdapter:
reason=STRANDED_RUN_ERROR,
)
except Exception as e:
- # Without the batches failed we'd fail the job while leaving claimable stragglers
- # behind — the exact resurrection this ordering prevents. Skip the rest for this run.
- logger.exception("stranded_run_batch_sweep_failed", run_uuid=ref.run_uuid)
- capture_exception(e)
+ if _is_transient_queue_connection_drop(e, conn):
+ logger.warning("stranded_run_batch_sweep_closed_connection", run_uuid=ref.run_uuid, error=str(e))
+ else:
+ # Without the batches failed we'd fail the job while leaving claimable stragglers
+ # behind — the exact resurrection this ordering prevents. Skip the rest for this run.
+ logger.exception("stranded_run_batch_sweep_failed", run_uuid=ref.run_uuid)
+ capture_exception(e)
continue
try:
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 c96becb873b2..999910578fcd 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
@@ -2170,6 +2170,65 @@ class TestReconcileFailedRuns:
assert mock_capture.called is expect_capture
+ @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_straggler_sweep_closed_connection_not_captured(self, conn_closed, expect_capture):
+ # Reproduces the reported issue: BatchQueue.fail_run for a straggler batch raised
+ # psycopg.OperationalError("consuming input failed: server closed the connection
+ # unexpectedly") because the queue-db connection died mid-query. That's the same
+ # transient network drop already handled for the stranded-run sweep above; the
+ # straggler sweep must treat it the same way instead of always capturing it.
+ consumer = _make_consumer()
+ ref = _make_failed_run_ref()
+
+ 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("consuming input failed: server closed the connection unexpectedly")
+
+ 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=[ref],
+ ),
+ patch(
+ f"{consumer_module.__name__}.BatchQueue.fail_run",
+ new_callable=AsyncMock,
+ side_effect=raise_with_maybe_closed_conn,
+ ),
+ patch(
+ f"{consumer_module.__name__}.BatchQueue.get_stale_stranded_runs",
+ new_callable=AsyncMock,
+ return_value=[],
+ ),
+ patch(
+ f"{consumer_module.__name__}.mark_job_failed_if_not_terminal",
+ return_value=False,
+ ),
+ patch(
+ f"{consumer_module.__name__}.release_v3_pipeline_lock",
+ ),
+ 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(