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): keep a job's real failure reason instead of "Cancelled" (#82530)

Tom Owerscbaa7e0743ac
diff --git a/products/data_warehouse/backend/logic/external_data_source/jobs.py b/products/data_warehouse/backend/logic/external_data_source/jobs.py
index a699a96f76b6..2b9ab03f4eb6 100644
--- a/products/data_warehouse/backend/logic/external_data_source/jobs.py
+++ b/products/data_warehouse/backend/logic/external_data_source/jobs.py
@@ -40,6 +40,9 @@ def update_external_job_status(
with transaction.atomic():
model = ExternalDataJob.objects.select_for_update().get(id=job_id, team_id=team_id)
+ was_already_terminal = model.status in TERMINAL_JOB_STATUSES
+ existing_error = model.latest_error
+
# The loader may finish a run after lock takeover force-failed its job; only the
# exact takeover sentinel unseals Failed -> Completed — genuine failures stay absorbing.
is_takeover_recovery = (
@@ -66,7 +69,16 @@ def update_external_job_status(
)
model.status = status
- model.latest_error = latest_error
+
+ # Once a job records a terminal failure reason, a later write of the SAME terminal status
+ # must not overwrite it. When a schema auto-disables, its teardown force-fails every other
+ # still-running job of that schema with the real reason, then cancels their workflows;
+ # Temporal's cancellation re-finalizes each as FAILED with a bare "Cancelled", which would
+ # otherwise clobber the actionable error. Takeover recovery (Failed -> Completed) is exempt
+ # — it deliberately replaces the sentinel with the successful run's empty error.
+ preserve_existing_error = was_already_terminal and not is_takeover_recovery and existing_error is not None
+ error_to_persist = existing_error if preserve_existing_error else latest_error
+ model.latest_error = error_to_persist
# Only stamp finished_at and emit metrics on the first terminal transition. Takeover
# recovery re-stamps: the Failed stamp predates the load and never emitted success metrics.
@@ -104,7 +116,7 @@ def update_external_job_status(
)
else:
schema.status = schema_status
- schema.latest_error = latest_error
+ schema.latest_error = error_to_persist
schema.save(update_fields=["status", "latest_error", "updated_at"])
# Every risky terminal write (any non-Completed terminal, plus the takeover-recovery
diff --git a/products/data_warehouse/backend/logic/external_data_source/test/test_jobs.py b/products/data_warehouse/backend/logic/external_data_source/test/test_jobs.py
index 71cd0cf764ca..8ad02fd4c7dd 100644
--- a/products/data_warehouse/backend/logic/external_data_source/test/test_jobs.py
+++ b/products/data_warehouse/backend/logic/external_data_source/test/test_jobs.py
@@ -269,6 +269,35 @@ class TestUpdateExternalJobStatus:
expected_error = "first error" if first_status == ExternalDataJob.Status.FAILED else None
assert db_job.latest_error == expected_error
+ def test_terminal_error_not_clobbered_by_later_cancelled_finalization(self):
+ # A schema auto-disable force-fails a still-running sibling job with the real reason, then
+ # cancels its workflow; Temporal re-finalizes that job as FAILED with a bare "Cancelled".
+ # That later same-status write must not overwrite the actionable error on the job or schema.
+ team, _source, schema, job = _create_org_team_source_schema_job()
+
+ with patch("products.data_warehouse.backend.logic.external_data_source.jobs.emit_data_import_app_metrics"):
+ update_external_job_status(
+ job_id=str(job.id),
+ team_id=team.pk,
+ status=ExternalDataJob.Status.FAILED,
+ logger=MagicMock(),
+ latest_error="Tenant or user not found",
+ )
+
+ updated = update_external_job_status(
+ job_id=str(job.id),
+ team_id=team.pk,
+ status=ExternalDataJob.Status.FAILED,
+ logger=MagicMock(),
+ latest_error="Cancelled",
+ )
+
+ assert updated.latest_error == "Tenant or user not found"
+ db_job = ExternalDataJob.objects.get(id=job.id)
+ assert db_job.latest_error == "Tenant or user not found"
+ schema.refresh_from_db()
+ assert schema.latest_error == "Tenant or user not found"
+
def test_completed_after_lock_takeover_failure_is_allowed(self):
# A job force-failed by lock takeover while the loader was still working its run
# must accept the loader's completion instead of rejecting Failed -> Completed.