: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(mailerlite): retry transient chunked-encoding read errors (#63987)
Tom Owersd158896d4c6c
diff --git a/posthog/temporal/data_imports/sources/mailerlite/mailerlite.py b/posthog/temporal/data_imports/sources/mailerlite/mailerlite.pyindex e63d0044dade..08de6dd41484 100644--- a/posthog/temporal/data_imports/sources/mailerlite/mailerlite.py+++ b/posthog/temporal/data_imports/sources/mailerlite/mailerlite.py@@ -4,6 +4,7 @@ from typing import Anyfrom urllib.parse import urlencodeimport requests+from requests.exceptions import ChunkedEncodingErrorfrom structlog.types import FilteringBoundLoggerfrom tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter@@ -71,7 +72,13 @@ def get_rows(url = _build_initial_url(config.path)@retry(- retry=retry_if_exception_type((MailerLiteRetryableError, requests.ReadTimeout, requests.ConnectionError)),+ # ChunkedEncodingError is a transient mid-stream connection drop while reading the+ # response body (e.g. "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.+ retry=retry_if_exception_type(+ (MailerLiteRetryableError, requests.ReadTimeout, requests.ConnectionError, ChunkedEncodingError)+ ),stop=stop_after_attempt(5),wait=wait_exponential_jitter(initial=1, max=60),reraise=True,diff --git a/posthog/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.py b/posthog/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.pyindex 3af234f20474..11f5afc3fd13 100644--- a/posthog/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.py+++ b/posthog/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.py@@ -1,11 +1,12 @@import json+from collections.abc import Iteratorfrom typing import Anyimport pytestfrom unittest.mock import MagicMock, patchfrom requests import Response-from requests.exceptions import HTTPError+from requests.exceptions import ChunkedEncodingError, HTTPErrorfrom posthog.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom posthog.temporal.data_imports.sources.mailerlite.mailerlite import (@@ -124,6 +125,50 @@ class TestGetRows:assert batches == []manager.save_state.assert_not_called()+ def test_chunked_encoding_error_is_retried(self) -> None:+ # 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.+ manager = MagicMock(spec=ResumableSourceManager)+ manager.can_resume.return_value = False++ good_response = _make_response(_page([{"id": "1"}], None))+ attempts: Iterator[ChunkedEncodingError | Response] = iter(+ [+ ChunkedEncodingError("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)"),+ good_response,+ ]+ )++ def fake_get(url: str, *_args: Any, **_kwargs: Any) -> Response:+ result = next(attempts)+ if isinstance(result, Exception):+ raise result+ return result++ with (+ patch("posthog.temporal.data_imports.sources.mailerlite.mailerlite.make_tracked_session") as MockSession,+ patch("tenacity.nap.time.sleep"),+ ):+ MockSession.return_value.get.side_effect = fake_get+ batches = list(get_rows("test-key", "subscribers", MagicMock(), manager))++ assert batches == [[{"id": "1"}]]+ assert MockSession.return_value.get.call_count == 2++ def test_chunked_encoding_error_eventually_reraises(self) -> None:+ manager = MagicMock(spec=ResumableSourceManager)+ manager.can_resume.return_value = False++ with (+ patch("posthog.temporal.data_imports.sources.mailerlite.mailerlite.make_tracked_session") as MockSession,+ patch("tenacity.nap.time.sleep"),+ ):+ MockSession.return_value.get.side_effect = ChunkedEncodingError("Connection broken")+ with pytest.raises(ChunkedEncodingError):+ list(get_rows("test-key", "subscribers", MagicMock(), manager))++ assert MockSession.return_value.get.call_count == 5+def test_non_retryable_status_raises(self) -> None:manager = MagicMock(spec=ResumableSourceManager)manager.can_resume.return_value = False