: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-imports): stop retrying a 404 from a REST-based source (#78441)
Tom Owersa4db908f85ea
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.pyindex 3b19a147bdd5..6401ca307d43 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@@ -8,6 +8,7 @@ from typing import Any, NoReturn, Optionalfrom django.db import InterfaceError, OperationalErrorfrom django.db.models import Prefetch+from requests.exceptions import HTTPErrorfrom structlog.contextvars import bind_contextvarsfrom structlog.typing import FilteringBoundLoggerfrom temporalio import activity@@ -391,6 +392,16 @@ async def _handle_import_error(job_inputs.team_id, str(job_inputs.source_id), job_inputs.run_id, error_msg, logger, error)+ # A 404 from the shared REST engine's fallback `raise_for_status()` path means the configured+ # endpoint/resource doesn't exist — every retry replays the identical request against the same+ # dead URL. Unlike 401 (a token needing refresh, which the REST engine's own retry re-mints) or+ # 429/5xx (already RESTClientRetryableError), there's no self-recovering path for a 404, so+ # classify it here rather than depending on each REST-based source listing it.+ if isinstance(error, HTTPError) and error.response is not None and error.response.status_code == 404:+ await handle_non_retryable_error(+ job_inputs.team_id, str(job_inputs.source_id), job_inputs.run_id, error_msg, logger, error+ )+# Raised in shared pipeline code when incoming data can't be cast into the stored (narrower)# Delta column type. delta-rs can't change a column's type in place, so this fails identically# on every retry regardless of source — classify it non-retryable by type here rather thandiff --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.pyindex 2ea979668d20..26bbbd6db81d 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@@ -10,6 +10,7 @@ from unittest import mockfrom django.db import InterfaceError, OperationalErrorfrom parameterized import parameterized+from requests.exceptions import HTTPErrorfrom posthog.temporal.common.errors import NonReportableError@@ -229,6 +230,68 @@ async def test_rest_client_non_retryable_error_routes_through_handler_without_sologger.aexception.assert_not_awaited()+async def test_http_404_routes_through_handler_without_source_opt_in():+ # A 404 from the shared REST engine's fallback raise_for_status() path means the configured+ # endpoint/resource doesn't exist — every retry hits the identical dead URL. It must be honored+ # by status code even when the source's get_non_retryable_errors doesn't list the message, so+ # any REST-based source stops instead of retrying to the activity max and minting error-tracking+ # noise on every attempt.+ error = HTTPError(+ "404 Client Error: Not Found for url: https://api.example.com/export", response=mock.MagicMock(status_code=404)+ )+ source = mock.MagicMock(spec=SimpleSource)+ source.get_non_retryable_errors.return_value = {}+ source.get_retryable_errors.return_value = set()++ logger = mock.MagicMock()+ logger.awarning = mock.AsyncMock()+ logger.aexception = mock.AsyncMock()+ logger.adebug = mock.AsyncMock()++ with (+ mock.patch.object(module.SourceRegistry, "get_source", return_value=source),+ mock.patch.object(module, "handle_non_retryable_error", new=mock.AsyncMock()) as handle_mock,+ ):+ handle_mock.side_effect = NonRetryableException()+ with pytest.raises(NonRetryableException):+ await module._handle_import_error(mock.MagicMock(), logger, error)++ handle_mock.assert_awaited_once()+ assert handle_mock.await_args is not None+ assert handle_mock.await_args.args[5] is error+ logger.aexception.assert_not_awaited()+++async def test_http_401_is_reraised_for_activity_retry():+ # A 401 can mean an access token expired mid-run — the REST engine's own auth layer re-mints+ # the token on the next attempt, so this must stay retryable rather than being swept into the+ # same non-retryable bucket as a 404 (which has no self-recovering path).+ error = HTTPError(+ "401 Client Error: Unauthorized for url: https://api.example.com/export",+ response=mock.MagicMock(status_code=401),+ )+ source = mock.MagicMock(spec=SimpleSource)+ source.get_non_retryable_errors.return_value = {}+ source.get_retryable_errors.return_value = set()++ logger = mock.MagicMock()+ logger.awarning = mock.AsyncMock()+ logger.aexception = mock.AsyncMock()+ logger.adebug = mock.AsyncMock()++ with (+ mock.patch.object(module.SourceRegistry, "get_source", return_value=source),+ mock.patch.object(module, "handle_non_retryable_error", new=mock.AsyncMock()) as handle_mock,+ ):+ with pytest.raises(HTTPError):+ await module._handle_import_error(mock.MagicMock(), logger, error)++ handle_mock.assert_not_awaited()+ logger.aexception.assert_awaited_once()++@pytest.mark.asyncioasync def test_rest_client_retryable_error_logged_as_warning_without_source_opt_in():# RESTClientRetryableError only escapes the shared REST engine's tenacity retry loop once its