: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(klaviyo): retry mid-stream chunked-encoding connection breaks (#67319)
Tom Owers7838c9424aea
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/klaviyo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/klaviyo.pyindex d19a8fc2478c..602a3dfd2830 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/klaviyo.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/klaviyo.py@@ -163,7 +163,16 @@ def _build_initial_params(@retry(- retry=retry_if_exception_type((KlaviyoRetryableError, requests.ReadTimeout, requests.ConnectionError)),+ # ChunkedEncodingError is a mid-stream connection break (the server truncated a chunked+ # response body); it's transient like ConnectionError/ReadTimeout, not a ConnectionError subclass.+ retry=retry_if_exception_type(+ (+ KlaviyoRetryableError,+ requests.ReadTimeout,+ requests.ConnectionError,+ requests.exceptions.ChunkedEncodingError,+ )+ ),stop=stop_after_attempt(5),wait=wait_exponential_jitter(initial=1, max=30),reraise=True,diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.pyindex 985b5541a23e..a2bcf2c6282a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.py@@ -3,7 +3,7 @@ from typing import Anyimport pytestfrom freezegun import freeze_time-from unittest.mock import MagicMock+from unittest.mock import MagicMock, patchimport requestsfrom parameterized import parameterized@@ -193,6 +193,42 @@ class TestNonRetryableErrors:assert not any(key in other_error for key in non_retryable_errors)+class TestFetchPageRetries:+ @parameterized.expand(+ [+ ("chunked_encoding", requests.exceptions.ChunkedEncodingError("Connection broken: InvalidChunkLength")),+ ("read_timeout", requests.ReadTimeout("Read timed out.")),+ ("connection_error", requests.ConnectionError("Connection reset by peer")),+ ]+ )+ def test_transient_errors_are_retried(self, _name: str, transient_error: Exception) -> None:+ # A transient network failure on the first attempt must retry rather than fail the whole sync.+ good = MagicMock()+ good.status_code = 200+ good.ok = True+ good.json.return_value = {"data": []}++ session = MagicMock()+ session.get.side_effect = [transient_error, good]++ with patch.object(klaviyo._fetch_page.retry, "sleep", lambda *_: None): # type: ignore[attr-defined]+ result = klaviyo._fetch_page(session, "https://a.klaviyo.com/api/events", {}, MagicMock())++ assert result == {"data": []}+ assert session.get.call_count == 2++ def test_transient_error_reraised_after_exhausting_attempts(self) -> None:+ # After the 5-attempt cap the last transient error must surface (reraise=True), not be swallowed.+ session = MagicMock()+ session.get.side_effect = requests.exceptions.ChunkedEncodingError("Connection broken: InvalidChunkLength")++ with patch.object(klaviyo._fetch_page.retry, "sleep", lambda *_: None): # type: ignore[attr-defined]+ with pytest.raises(requests.exceptions.ChunkedEncodingError):+ klaviyo._fetch_page(session, "https://a.klaviyo.com/api/events", {}, MagicMock())++ assert session.get.call_count == 5++def _response_with_status(status_code: int) -> requests.Response:response = requests.Response()response.status_code = status_code