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 a redirect loop in the shared rest client (#112175)

Tom Owersf8b1779db4be
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py
index 44657dd5486c..af2e8059c240 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py
@@ -15,6 +15,7 @@ from requests.exceptions import (
HTTPError,
JSONDecodeError as RequestsJSONDecodeError,
Timeout as RequestsTimeout,
+ TooManyRedirects,
)
from tenacity import RetryCallState, retry, retry_if_exception_type
@@ -536,6 +537,15 @@ class RESTClient:
raise RESTClientRetryableError(
self._redact(f"Request timed out ({type(e).__name__}) for {_safe_url(prepared.url or '')}")
) from e
+ except TooManyRedirects as e:
+ # requests already followed its redirect cap (30) chasing a final response and never
+ # got one — a deterministic loop (e.g. an auth wall, or an http/https scheme mismatch)
+ # baked into how the remote host answers this URL, not a one-off network blip.
+ # Re-fetching replays the same chain, so fail fast and non-retryably instead of burning
+ # the retry budget on 30+ requests per attempt.
+ raise RESTClientNonRetryableError(
+ self._redact(f"Too many redirects for {_safe_url(prepared.url or '')}")
+ ) from e
# With redirects disabled, a 3xx is not an error to `raise_for_status` and would fall
# through to JSON parsing; reject it explicitly so a redirect can't smuggle the request
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py
index 2dcc380ea3d9..078a0d8b02fa 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py
@@ -7,7 +7,7 @@ from unittest.mock import MagicMock, patch
from parameterized import parameterized
from requests import Response
-from requests.exceptions import ChunkedEncodingError, ProxyError, ReadTimeout
+from requests.exceptions import ChunkedEncodingError, ProxyError, ReadTimeout, TooManyRedirects
from posthog.temporal.common.errors import NonReportableError
@@ -449,6 +449,30 @@ class TestRESTClient:
assert "api_key" not in message
assert "https://api.example.com/leads" in message
+ @patch("tenacity.nap.time.sleep")
+ @patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client.make_tracked_session"
+ )
+ def test_too_many_redirects_is_non_retryable(self, MockSession, mock_sleep) -> None:
+ # `requests` already exhausted its own redirect cap (30) chasing a final response and
+ # never got one — a deterministic loop baked into how the host answers this URL.
+ # Re-fetching replays the same chain, so this must fail fast rather than retrying to the
+ # tenacity cap (contrast the connection/timeout cases above, which stay retryable).
+ mock_session = MockSession.return_value
+ mock_session.headers = {}
+ mock_session.prepare_request.return_value = MagicMock(url="https://api.example.com/items")
+ mock_session.send.side_effect = TooManyRedirects("Exceeded 30 redirects.")
+
+ client = RESTClient(base_url="https://api.example.com")
+ with pytest.raises(RESTClientNonRetryableError, match="Too many redirects") as ctx:
+ list(client.paginate(path="/items", paginator=SinglePagePaginator()))
+
+ assert mock_session.send.call_count == 1
+ mock_sleep.assert_not_called()
+ # A redirect loop is always a customer/upstream condition, so it must carry the
+ # non-reportable marker the activity interceptor uses to keep it out of error tracking.
+ assert isinstance(ctx.value, NonReportableError)
+
@pytest.mark.parametrize("content", [b"", b" \n\t"], ids=["empty", "whitespace_only"])
@patch("tenacity.nap.time.sleep")
@patch(