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 retrying an import after its schema is deleted mid-sync (#78871)

Tom Owersd62c3ec378be
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py
index 6401ca307d43..fe7610559078 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py
@@ -185,7 +185,10 @@ async def _import_data_with_reporting(inputs: ImportDataActivityInputs, logger:
await logger.adebug(f"schema.sync_type_config = {schema.sync_type_config}")
await logger.adebug(f"reset_pipeline = {reset_pipeline}")
- schema = await _get_external_data_schema(inputs.schema_id, inputs.team_id)
+ try:
+ schema = await _get_external_data_schema(inputs.schema_id, inputs.team_id)
+ except ExternalDataSchema.DoesNotExist as e:
+ await _handle_import_error(job_inputs, logger, e)
processed_incremental_last_value = None
processed_incremental_earliest_value = None
@@ -383,6 +386,15 @@ async def _handle_import_error(
source_cls = SourceRegistry.get_source(job_inputs.job_type)
error_msg = str(error)
+ # The schema this activity is running for can be deleted (or soft-deleted) between the job
+ # being created and this activity's mid-run re-fetch of it — every retry re-reads the same
+ # gone row, so it never turns into data regardless of source. Classify it here by type rather
+ # than depending on each source listing the message in get_non_retryable_errors.
+ if isinstance(error, ExternalDataSchema.DoesNotExist):
+ await handle_non_retryable_error(
+ job_inputs.team_id, str(job_inputs.source_id), job_inputs.run_id, error_msg, logger, error
+ )
+
# The shared REST engine raises RESTClientNonRetryableError only for responses retrying can
# never turn into data (a non-JSON body on an otherwise-successful response). Honor that
# contract by type so every REST-based source stops immediately, rather than depending on each
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py
index 26bbbd6db81d..41a788a5a27d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py
@@ -172,6 +172,29 @@ async def test_unparseable_config_routes_through_handler():
source.source_for_pipeline.assert_not_called()
+async def test_schema_deleted_mid_sync_routes_through_handler():
+ # The schema can be deleted (or soft-deleted) between the job being created and this
+ # activity's mid-run re-fetch of it, e.g. a user removes the table while its sync is in
+ # flight. Every retry re-reads the same gone row, so it must be treated as non-retryable
+ # instead of crash-looping on every attempt.
+ error = ExternalDataSchema.DoesNotExist("ExternalDataSchema matching query does not exist.")
+ source = mock.MagicMock(spec=SimpleSource)
+ source.get_non_retryable_errors.return_value = {}
+
+ with (
+ _patched_activity(source) as handle_mock,
+ mock.patch.object(module, "_get_external_data_schema", new=mock.AsyncMock(side_effect=error)),
+ ):
+ handle_mock.side_effect = NonRetryableException()
+ with pytest.raises(NonRetryableException):
+ await import_data_activity_sync(_inputs())
+
+ handle_mock.assert_awaited_once()
+ assert handle_mock.await_args.args[5] is error
+ source.parse_config.assert_not_called()
+
+
@pytest.mark.asyncio
async def test_source_classified_retryable_error_logged_as_warning_not_exception():
# A rate-limit / transient error the source retries internally reaches _handle_import_error only