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_search_console): retry truncated Search Analytics response bodies (#89799)

Tom Owersa0c6884643d6
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py
index 56510a276b24..56aac2128704 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py
@@ -376,7 +376,25 @@ def _query_search_analytics(
continue
if response.ok:
- return response.json().get("rows", [])
+ try:
+ return response.json().get("rows", [])
+ except requests.exceptions.ChunkedEncodingError:
+ # The body streams via chunked transfer-encoding and can still be cut off
+ # mid-read after a 200 with a good `response` object — a dropped connection
+ # after headers rather than before, so it lands here instead of the
+ # ConnectionError/Timeout handling above. Same transient class; retry inline,
+ # then let Temporal retry the activity once the inline budget is spent.
+ if attempt == QUOTA_MAX_RETRIES:
+ raise
+ wait = QUOTA_BACKOFF_BASE_SECONDS * (2**attempt)
+ logger.warning(
+ "GSC response body truncated, backing off",
+ site_url=site_url,
+ attempt=attempt,
+ wait_seconds=wait,
+ )
+ time.sleep(wait)
+ continue
# Surface Google's real reason (e.g. usageLimits/quotaExceeded vs forbidden) —
# raise_for_status() discards the body where that distinction lives.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py
index 724de2036943..ee4cf6df7cea 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console.py
@@ -880,6 +880,41 @@ def test_query_read_timeout_bubbles_after_max_retries(monkeypatch):
assert session.post.call_count == QUOTA_MAX_RETRIES + 1
+def test_query_retries_truncated_body_then_succeeds(monkeypatch):
+ monkeypatch.setattr(gsc.time, "sleep", lambda _s: None)
+ monkeypatch.setattr(gsc, "_throttle", lambda _site: None)
+
+ truncated = _fake_response(200)
+ truncated.json.side_effect = requests.exceptions.ChunkedEncodingError("Connection broken: IncompleteRead(...)")
+ session = mock.MagicMock()
+ session.post.side_effect = [
+ truncated,
+ _fake_response(200, {"rows": [{"keys": ["2026-04-15"], "clicks": 1}]}),
+ ]
+
+ rows = _query_search_analytics(session, "sc-domain:example.com", "2026-04-15", "2026-04-15", ["date"], 0)
+
+ assert rows == [{"keys": ["2026-04-15"], "clicks": 1}]
+ assert session.post.call_count == 2
+
+
+def test_query_truncated_body_bubbles_after_max_retries(monkeypatch):
+ monkeypatch.setattr(gsc.time, "sleep", lambda _s: None)
+ monkeypatch.setattr(gsc, "_throttle", lambda _site: None)
+
+ truncated = _fake_response(200)
+ truncated.json.side_effect = requests.exceptions.ChunkedEncodingError("Connection broken: IncompleteRead(...)")
+ session = mock.MagicMock()
+ session.post.return_value = truncated
+
+ # A persistently truncated response body (chunked transfer cut off mid-read) exhausts the
+ # inline budget and surfaces the real ChunkedEncodingError (retryable at the activity level).
+ with pytest.raises(requests.exceptions.ChunkedEncodingError):
+ _query_search_analytics(session, "sc-domain:example.com", "2026-04-15", "2026-04-15", ["date"], 0)
+
+ assert session.post.call_count == QUOTA_MAX_RETRIES + 1
+
+
def test_query_retries_transient_token_refresh_error_then_succeeds(monkeypatch):
monkeypatch.setattr(gsc.time, "sleep", lambda _s: None)
monkeypatch.setattr(gsc, "_throttle", lambda _site: None)