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): stop re-raising job-deleted race as a duplicate error (#74024)

Tom Owerseb9c7afe42f7
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 0778a8e22eca..e8acf1503d32 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
@@ -97,6 +97,10 @@ EXPECTED_USER_ERROR_PATTERNS: tuple[str, ...] = (
# (SchemaColumnTypeChangedException) — delta-rs can't widen in place, so the fix is a
# user-driven reset and full re-sync, not a code change
"Source column type changed",
+ # the schema or job was deleted (e.g. the user removed the source) while a batch for it
+ # was still in flight — an upstream/customer action, not a pipeline bug
+ "ExternalDataSchema matching query does not exist",
+ "ExternalDataJob matching query does not exist",
)
# How long an "alive" job-status lookup stays cached before re-checking the app DB.
@@ -646,13 +650,18 @@ def _update_job_status_to_failed(*, job_id: str, team_id: int, error: str) -> No
if existing is not None:
return
- update_external_job_status(
- job_id=job_id,
- team_id=team_id,
- status=ExternalDataJob.Status.FAILED,
- logger=structlog.get_logger(),
- latest_error=error,
- )
+ try:
+ update_external_job_status(
+ job_id=job_id,
+ team_id=team_id,
+ status=ExternalDataJob.Status.FAILED,
+ logger=structlog.get_logger(),
+ latest_error=error,
+ )
+ except ExternalDataJob.DoesNotExist:
+ # The job row itself was deleted between the check above and this write (e.g. its
+ # source/schema was removed mid-sync) — nothing left to mark failed.
+ pass
def mark_job_failed_if_not_terminal(*, job_id: str, team_id: int, error: str) -> bool:
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 96158cbf5acc..85d2ed16872f 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
@@ -8,6 +8,7 @@ from unittest.mock import AsyncMock, patch
import psycopg
import structlog
+from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob
from products.warehouse_sources.backend.temporal.data_imports.metrics import LOCK_TAKEOVER_LATEST_ERROR
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3 import (
batch_consumer as batch_consumer_module,
@@ -21,6 +22,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
ConsumerConfig,
DeltaBatchConsumerAdapter,
_group_by_key,
+ _update_job_status_to_failed,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.jobs_db import (
FRESHNESS_WINDOW_SECONDS,
@@ -183,6 +185,10 @@ class TestProcessSingle:
# fits the stored type): the run fails with an actionable message but stays out of
# error tracking.
("Source column type changed: 'price' has values that no longer fit its stored type int64", False),
+ # The job or schema was deleted mid-sync (e.g. the source was removed) — also an
+ # upstream/customer condition, not a pipeline bug.
+ ("ExternalDataJob matching query does not exist.", False),
+ ("ExternalDataSchema matching query does not exist.", False),
# A genuine non-retryable failure must still surface so real bugs aren't hidden.
("20009.59 is too large to store in a Decimal128 of precision 24.", True),
],
@@ -927,6 +933,26 @@ class TestFailRun:
mock_status.assert_called_once()
+class TestUpdateJobStatusToFailed:
+ def test_swallows_does_not_exist_when_job_deleted_mid_sync(self):
+ # The job row can vanish (e.g. its source was removed) between the "not already
+ # failed" check and the write below — this must be a no-op, not a second
+ # DoesNotExist raised on top of the batch's original failure.
+ with (
+ patch(
+ "products.warehouse_sources.backend.models.external_data_job.ExternalDataJob.objects.filter",
+ ) as mock_filter,
+ patch(
+ "products.data_warehouse.backend.facade.api.update_external_job_status",
+ side_effect=ExternalDataJob.DoesNotExist,
+ ) as mock_update,
+ ):
+ mock_filter.return_value.first.return_value = None
+ _update_job_status_to_failed(job_id="job-1", team_id=1, error="boom")
+
+ mock_update.assert_called_once()
+
+
class TestShouldProcessBatch:
@pytest.mark.parametrize("job_status", ["Failed", "BillingLimitReached"], ids=["failed", "billing_limited"])
@pytest.mark.asyncio