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-warehouse): always retry v3 lock release in the reconcile sweep (#72344)

estefaniabf0e835ab3a8
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 c9b13eba5458..7e96aa0cc28b 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
@@ -320,21 +320,20 @@ class DeltaBatchConsumerAdapter:
except Exception as e:
logger.exception("reconcile_job_status_update_failed", job_id=ref.job_id, run_uuid=ref.run_uuid)
capture_exception(e)
- continue
-
- if not reconciled:
- continue # job was already terminal — nothing to reconcile
-
- RUNS_RECONCILED_TOTAL.inc()
- logger.warning(
- "run_reconciled_to_failed",
- job_id=ref.job_id,
- run_uuid=ref.run_uuid,
- team_id=ref.team_id,
- external_data_schema_id=ref.schema_id,
- )
+ reconciled = False
+
+ if reconciled:
+ RUNS_RECONCILED_TOTAL.inc()
+ logger.warning(
+ "run_reconciled_to_failed",
+ job_id=ref.job_id,
+ run_uuid=ref.run_uuid,
+ team_id=ref.team_id,
+ external_data_schema_id=ref.schema_id,
+ )
- # Release the V3 pipeline lock too, otherwise it blocks the schema's next sync until its TTL expires.
+ # Attempted for every ref: this sweep is the retry for a fail_run whose own release
+ # failed silently. Safe to repeat, since the release compare-and-deletes on the token.
if ref.workflow_run_id:
try:
await sync_to_async(release_v3_pipeline_lock)(
@@ -350,6 +349,13 @@ class DeltaBatchConsumerAdapter:
exc_info=True,
)
capture_exception(e)
+ else:
+ logger.info(
+ "v3_pipeline_lock_release_skipped_no_workflow_run_id",
+ job_id=ref.job_id,
+ run_uuid=ref.run_uuid,
+ external_data_schema_id=ref.schema_id,
+ )
async def _observe_queue_freshness(self, conn: psycopg.AsyncConnection[Any]) -> None:
"""Report the age of the oldest batch no consumer has picked up yet.
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 7c4bf21e0eb1..a0c37e7e3448 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
@@ -28,6 +28,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.metrics import (
OLDEST_UNCLAIMED_BATCH_SECONDS,
+ RUNS_RECONCILED_TOTAL,
)
from products.warehouse_sources_queue.backend.models import SourceBatchStatus
@@ -1002,6 +1003,7 @@ class TestReconcileFailedRuns:
consumer = _make_consumer()
ref = _make_failed_run_ref()
+ reconciled_before = RUNS_RECONCILED_TOTAL._value.get()
with (
patch(
"products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_failed_runs",
@@ -1020,6 +1022,7 @@ class TestReconcileFailedRuns:
mock_mark.assert_called_once_with(job_id=ref.job_id, team_id=ref.team_id, error=ref.reason)
mock_release.assert_called_once_with(team_id=ref.team_id, schema_id=ref.schema_id, token=ref.workflow_run_id)
+ assert RUNS_RECONCILED_TOTAL._value.get() == reconciled_before + 1
@pytest.mark.asyncio
async def test_sweeps_stragglers_even_for_already_terminal_run(self):
@@ -1056,11 +1059,19 @@ class TestReconcileFailedRuns:
assert mock_fail_run.call_args.kwargs["team_id"] == ref.team_id
assert mock_fail_run.call_args.kwargs["schema_id"] == ref.schema_id
+ @pytest.mark.parametrize(
+ "mark_outcome",
+ [False, Exception("db down")],
+ ids=["already_terminal", "status_update_raises"],
+ )
@pytest.mark.asyncio
- async def test_skips_already_terminal_run(self):
+ async def test_releases_lock_even_when_job_not_reconciled(self, mark_outcome):
+ # The sweep is the retry for a fail_run whose own lock release failed silently,
+ # so the release must be attempted regardless of the reconcile outcome.
consumer = _make_consumer()
ref = _make_failed_run_ref()
+ reconciled_before = RUNS_RECONCILED_TOTAL._value.get()
with (
patch(
"products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_failed_runs",
@@ -1069,7 +1080,7 @@ class TestReconcileFailedRuns:
),
patch(
"products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.mark_job_failed_if_not_terminal",
- return_value=False,
+ side_effect=[mark_outcome],
) as mock_mark,
patch(
"products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.release_v3_pipeline_lock",
@@ -1077,8 +1088,32 @@ class TestReconcileFailedRuns:
):
await consumer._reconcile_failed_runs()
- mock_mark.assert_called_once() # no-op for an already-terminal job, no error
- mock_release.assert_not_called() # don't release the lock for a job we didn't reconcile
+ mock_mark.assert_called_once()
+ mock_release.assert_called_once_with(team_id=ref.team_id, schema_id=ref.schema_id, token=ref.workflow_run_id)
+ assert RUNS_RECONCILED_TOTAL._value.get() == reconciled_before # nothing reconciled
+
+ @pytest.mark.asyncio
+ async def test_no_release_attempt_without_workflow_run_id(self):
+ consumer = _make_consumer()
+ ref = _make_failed_run_ref(workflow_run_id=None)
+
+ with (
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.BatchQueue.get_failed_runs",
+ new_callable=AsyncMock,
+ return_value=[ref],
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.mark_job_failed_if_not_terminal",
+ return_value=True,
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.consumer.release_v3_pipeline_lock",
+ ) as mock_release,
+ ):
+ await consumer._reconcile_failed_runs()
+
+ mock_release.assert_not_called() # no token means nothing to release
@pytest.mark.asyncio
async def test_continues_after_error_on_one_ref(self):