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(google_pagespeed_insights): retry connection-broken chunked encoding errors (#72075)

Tom Owers69c222db2de4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py
index 6fe7ba905111..25810523203f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py
@@ -140,8 +140,20 @@ def _redact_key(text: str) -> str:
return _KEY_RE.sub(r"\1REDACTED", text)
+# `ChunkedEncodingError` and `ContentDecodingError` are siblings of `ConnectionError` under
+# `RequestException`, not subclasses, so they must be listed explicitly: a connection broken
+# mid-response (server hung up before the body was fully streamed) or a body that fails to decode is
+# transient and safe to retry on the large PageSpeed payloads, not a reason to fail the whole sync.
@retry(
- retry=retry_if_exception_type((PageSpeedRetryableError, requests.ReadTimeout, requests.ConnectionError)),
+ retry=retry_if_exception_type(
+ (
+ PageSpeedRetryableError,
+ requests.ReadTimeout,
+ requests.ConnectionError,
+ requests.exceptions.ChunkedEncodingError,
+ requests.exceptions.ContentDecodingError,
+ )
+ ),
stop=stop_after_attempt(MAX_RETRY_ATTEMPTS),
wait=wait_exponential_jitter(initial=2, max=60),
reraise=True,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py
index 1c4c4895cfa4..02eeabc2d871 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py
@@ -234,6 +234,30 @@ class TestFetch:
# The host prefix is preserved so non-retryable-error matching still works.
assert "for url: https://pagespeedonline.googleapis.com" in message
+ @pytest.mark.parametrize(
+ "exc",
+ [
+ # A connection broken mid-response surfaces as ChunkedEncodingError, and a body that fails
+ # to decode as ContentDecodingError. Both subclass RequestException directly (not
+ # ConnectionError), so they must be retried rather than failing the sync on the first hiccup.
+ requests.exceptions.ChunkedEncodingError(
+ "Connection broken: InvalidChunkLength(got length b'', 0 bytes read)"
+ ),
+ requests.exceptions.ContentDecodingError(
+ "Received response with content-encoding: gzip, but failed to decode it."
+ ),
+ ],
+ )
+ def test_body_read_errors_are_retried(self, exc):
+ session = mock.MagicMock()
+ session.get.side_effect = [exc, _response(200, {"analysisUTCTimestamp": "2024-01-15T12:34:56Z"})]
+
+ with mock.patch.object(_fetch.retry, "sleep"): # type: ignore[attr-defined]
+ body = _fetch(session, "k", "DESKTOP", "https://posthog.com", structlog.get_logger())
+
+ assert body == {"analysisUTCTimestamp": "2024-01-15T12:34:56Z"}
+ assert session.get.call_count == 2
+
@pytest.mark.parametrize("exc_type", [requests.ConnectionError, requests.ReadTimeout, requests.exceptions.SSLError])
def test_transport_error_redacts_key_and_preserves_type(self, exc_type):
# Transport failures (no HTTP response) embed the full request URL, including `key=...`, in