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 reporting queue-sweep cancellations as failures (#79794)

Tom Owers7daaeb44d0e5
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 19a62d67b951..a699a96f76b6 100644
--- a/products/data_warehouse/backend/logic/external_data_source/jobs.py
+++ b/products/data_warehouse/backend/logic/external_data_source/jobs.py
@@ -1,3 +1,4 @@
+import asyncio
import datetime as dt
from functools import partial
@@ -136,6 +137,16 @@ def update_external_job_status(
return model
+def _is_cancellation(error: BaseException) -> bool:
+ """Whether `error` is an activity cancellation, however it surfaced.
+
+ Temporal can inject a `temporalio.exceptions.CancelledError` (an `Exception` subclass) into the
+ thread this runs on when the calling activity is cancelled mid-sweep — match on the type name too
+ so it isn't mistaken for a genuine queue-DB failure.
+ """
+ return isinstance(error, asyncio.CancelledError) or type(error).__name__ == "CancelledError"
+
+
def _sweep_v3_queue_batches_swallowing_errors(
*, job_id: str, status: ExternalDataJob.Status, logger: FilteringBoundLogger
) -> None:
@@ -144,6 +155,10 @@ def _sweep_v3_queue_batches_swallowing_errors(
try:
_sweep_v3_queue_batches(job_id=job_id, status=status, logger=logger)
except Exception as e:
+ if _is_cancellation(e):
+ # Activity cancellation, not a sweep failure — propagate so the caller's own cancellation
+ # handling runs, instead of reporting it as `dwh_finalize_queue_sweep_failed` noise.
+ raise
FINALIZE_QUEUE_SWEEP_ERRORS.inc()
logger.exception("dwh_finalize_queue_sweep_failed", job_id=job_id)
capture_exception(e)
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 9891d91d7150..71cd0cf764ca 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
@@ -3,6 +3,8 @@ import uuid
import pytest
from unittest.mock import MagicMock, patch
+from temporalio.exceptions import CancelledError
+
from posthog.models import Organization, Team
from products.data_warehouse.backend.logic.external_data_source.jobs import update_external_job_status
@@ -502,3 +504,35 @@ class TestFinalizeQueueSweep:
db_job = ExternalDataJob.objects.get(id=job.id)
assert db_job.status == ExternalDataJob.Status.FAILED
assert db_job.finished_at is not None
+
+ def test_queue_sweep_cancellation_propagates_instead_of_being_reported(self, django_capture_on_commit_callbacks):
+ # Temporal can inject a CancelledError into the sweep's thread when the activity that
+ # triggered this status write is itself cancelled mid-sweep. That must propagate so the
+ # caller's own cancellation handling runs, not get swallowed and reported as a sweep bug.
+ team, _source, _schema, job = _create_org_team_source_schema_job(
+ pipeline_version=ExternalDataJob.PipelineVersion.V3
+ )
+
+ with (
+ patch("products.data_warehouse.backend.logic.external_data_source.jobs.emit_data_import_app_metrics"),
+ patch(
+ "products.data_warehouse.backend.logic.external_data_source.jobs.schedule_external_data_failure_digest"
+ ),
+ patch(
+ "products.data_warehouse.backend.logic.external_data_source.jobs._sweep_v3_queue_batches",
+ side_effect=CancelledError("Cancelled"),
+ ) as mock_sweep,
+ patch("products.data_warehouse.backend.logic.external_data_source.jobs.capture_exception") as mock_capture,
+ pytest.raises(CancelledError),
+ django_capture_on_commit_callbacks(execute=True),
+ ):
+ update_external_job_status(
+ job_id=str(job.id),
+ team_id=team.pk,
+ status=ExternalDataJob.Status.FAILED,
+ logger=MagicMock(),
+ latest_error="boom",
+ )
+
+ mock_sweep.assert_called_once()
+ mock_capture.assert_not_called()