: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(pinterest_ads): retry transient chunked-encoding connection drops (#65377)
Tom Owersc5fcf3b3139f
diff --git a/posthog/temporal/data_imports/sources/pinterest_ads/tests/test_pinterest_ads.py b/posthog/temporal/data_imports/sources/pinterest_ads/tests/test_pinterest_ads.pyindex f512a3afbef4..d0b437881fd8 100644--- a/posthog/temporal/data_imports/sources/pinterest_ads/tests/test_pinterest_ads.py+++ b/posthog/temporal/data_imports/sources/pinterest_ads/tests/test_pinterest_ads.py@@ -295,6 +295,37 @@ class TestMakeRequestErrorHandling:result = _make_request(mock_session, "https://api.pinterest.com/v5/test")assert result == {"items": []}+ def test_chunked_encoding_error_is_retried(self):+ # A mid-stream connection drop while reading the body raises ChunkedEncodingError, which must+ # be retried so a single dropped connection doesn't fail the whole import.+ good_response = mock.MagicMock()+ good_response.raise_for_status.return_value = None+ good_response.json.return_value = {"items": []}++ mock_session = mock.MagicMock()+ mock_session.get.side_effect = [+ requests.exceptions.ChunkedEncodingError(+ "Connection broken: InvalidChunkLength(got length b'', 0 bytes read)"+ ),+ good_response,+ ]++ with mock.patch("tenacity.nap.time.sleep"):+ result = _make_request(mock_session, "https://api.pinterest.com/v5/test")++ assert result == {"items": []}+ assert mock_session.get.call_count == 2++ def test_chunked_encoding_error_eventually_reraises(self):+ mock_session = mock.MagicMock()+ mock_session.get.side_effect = requests.exceptions.ChunkedEncodingError("Connection broken")++ with mock.patch("tenacity.nap.time.sleep"):+ with pytest.raises(requests.exceptions.ChunkedEncodingError):+ _make_request(mock_session, "https://api.pinterest.com/v5/test")++ assert mock_session.get.call_count == 5+class TestPinterestAdsSource:def test_unknown_endpoint(self):diff --git a/posthog/temporal/data_imports/sources/pinterest_ads/utils.py b/posthog/temporal/data_imports/sources/pinterest_ads/utils.pyindex 2c3aa9aa6de8..b57ccca70f28 100644--- a/posthog/temporal/data_imports/sources/pinterest_ads/utils.py+++ b/posthog/temporal/data_imports/sources/pinterest_ads/utils.py@@ -4,6 +4,8 @@ from typing import Any, Optionalimport requestsimport structlogfrom dateutil import parser+from requests.exceptions import ChunkedEncodingError+from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitterfrom posthog.temporal.data_imports.sources.common.http import make_tracked_sessionfrom posthog.temporal.data_imports.sources.pinterest_ads.settings import (@@ -85,6 +87,17 @@ def _chunk_date_range(start_date: str, end_date: str) -> list[tuple[str, str]]:return chunks+@retry(+ # ChunkedEncodingError is a transient mid-stream connection drop while reading the response+ # body ("Connection broken: InvalidChunkLength"). It subclasses RequestException directly, not+ # ConnectionError, so it must be listed explicitly to be retried rather than failing the whole+ # import on a single dropped connection. The tracked session only retries on status codes during+ # the initial request, not on a broken body stream.+ retry=retry_if_exception_type((requests.ReadTimeout, requests.ConnectionError, ChunkedEncodingError)),+ stop=stop_after_attempt(5),+ wait=wait_exponential_jitter(initial=1, max=60),+ reraise=True,+)def _make_request(session: requests.Session, url: str, params: Optional[dict] = None) -> Any:response = session.get(url, params=params, timeout=30)response.raise_for_status()