: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): unstick schema when cancelling a terminated sync (#73261)
Tom Owers1d13e960542e
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_schema.py b/products/warehouse_sources/backend/presentation/views/external_data_schema.pyindex fa811f41b9b9..eb3c376bea5b 100644--- a/products/warehouse_sources/backend/presentation/views/external_data_schema.py+++ b/products/warehouse_sources/backend/presentation/views/external_data_schema.py@@ -1511,16 +1511,30 @@ class ExternalDataSchemaViewset(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):)if latest_running_job.pipeline_version != ExternalDataJob.PipelineVersion.V3:- # v1/v2: the workflow itself owns the job's terminal status, so keep the legacy- # behavior where the cancel RPC is the whole operation and a missing workflow- # is an error.+ # v1/v2: normally the workflow handles the cancellation and writes the job's+ # terminal status itself, so the cancel RPC is the whole operation.try:cancel_external_data_workflow(latest_running_job.workflow_id)except temporalio.service.RPCError as e:- logger.exception(f"Could not cancel external data workflow for schema {instance.id}", exc_info=e)- return Response(- status=status.HTTP_400_BAD_REQUEST,- data={"detail": "Could not find workflow to cancel. The sync may have already finished."},+ if e.status != temporalio.service.RPCStatusCode.NOT_FOUND:+ # Transient RPC failure against a possibly-live workflow. The workflow still+ # owns the terminal status, so leave the job Running and surface the failure.+ logger.exception(f"Could not cancel external data workflow for schema {instance.id}", exc_info=e)+ return Response(+ status=status.HTTP_400_BAD_REQUEST,+ data={"detail": "Could not cancel the running sync. Please try again."},+ )+ # The workflow is already gone (e.g. it was terminated rather than cancelled), so it+ # will never run the cleanup that writes the terminal status - the job and schema+ # would stay stuck on Running forever. Write the Failed status ourselves so the+ # schema unsticks and can be synced again.+ logger.info("cancel_sync_v2_workflow_already_gone", schema_id=str(instance.id))+ update_external_job_status(+ job_id=str(latest_running_job.id),+ team_id=instance.team_id,+ status=ExternalDataJob.Status.FAILED,+ logger=logger,+ latest_error="Sync cancelled by user",)return Response(status=status.HTTP_200_OK)diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_schema.py b/products/warehouse_sources/backend/tests/api/test_external_data_schema.pyindex 86c1710fdadb..54f283931b28 100644--- a/products/warehouse_sources/backend/tests/api/test_external_data_schema.py+++ b/products/warehouse_sources/backend/tests/api/test_external_data_schema.py@@ -2954,21 +2954,50 @@ class TestCancelExternalDataSchema(APIBaseTest):assert job.status == ExternalDataJob.Status.RUNNINGassert job.latest_error is None+ @parameterized.expand([("v1", "v1-dlt-sync"), ("legacy_null_version", None)])@mock.patch("products.warehouse_sources.backend.presentation.views.external_data_schema.cancel_external_data_workflow")- def test_cancel_legacy_pipeline_returns_400_when_workflow_missing(self, mock_cancel):+ def test_cancel_legacy_pipeline_recovers_when_workflow_already_gone(self, _case, pipeline_version, mock_cancel):+ # A workflow that was terminated (not cancelled) never runs the cleanup that writes the+ # terminal status, so the cancel RPC comes back NOT_FOUND. Without recovery the job and+ # schema would stay stuck on Running forever and the schema could never be synced again.from temporalio.service import RPCError, RPCStatusCodefrom products.warehouse_sources.backend.facade.models import ExternalDataJob- schema, job = self._create_schema_with_running_job(pipeline_version=ExternalDataJob.PipelineVersion.V1)+ schema, job = self._create_schema_with_running_job(pipeline_version=pipeline_version)mock_cancel.side_effect = RPCError("workflow not found", RPCStatusCode.NOT_FOUND, b"")response = self.client.post(f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}/cancel/",)+ assert response.status_code == 200++ job.refresh_from_db()+ schema.refresh_from_db()+ assert job.status == ExternalDataJob.Status.FAILED+ assert job.latest_error == "Sync cancelled by user"+ assert schema.status == ExternalDataSchema.Status.FAILED++ @mock.patch(+ "products.warehouse_sources.backend.presentation.views.external_data_schema.cancel_external_data_workflow"+ )+ def test_cancel_legacy_pipeline_returns_400_on_transient_rpc_error(self, mock_cancel):+ # A transient RPC failure against a possibly-live workflow must not mark the job Failed -+ # the workflow still owns the terminal status, so leave it Running and surface the error.+ from temporalio.service import RPCError, RPCStatusCode++ from products.warehouse_sources.backend.facade.models import ExternalDataJob++ schema, job = self._create_schema_with_running_job(pipeline_version=ExternalDataJob.PipelineVersion.V1)+ mock_cancel.side_effect = RPCError("temporal unavailable", RPCStatusCode.UNAVAILABLE, b"")++ response = self.client.post(+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}/cancel/",+ )+assert response.status_code == 400job.refresh_from_db()