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 connection resets inline (#68691)

Tom Owers8b258a44e33d
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 837e1efdc227..ee239fff0872 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
@@ -304,7 +304,26 @@ def _query_search_analytics(
for attempt in range(QUOTA_MAX_RETRIES + 1):
_throttle(site_url)
- response = session.post(url, json=body)
+ try:
+ response = session.post(url, json=body)
+ except requests.ConnectionError:
+ # A dropped connection (RemoteDisconnected / connection reset) is raised before any
+ # response, so the quota/5xx handling below never sees it, and the tracked adapter's
+ # retry skips it because searchAnalytics.query is a POST. It's transient, so retry
+ # inline like a 5xx; once the inline budget is spent, let it bubble so Temporal
+ # retries the activity (resuming from the last saved date).
+ if attempt == QUOTA_MAX_RETRIES:
+ raise
+ wait = QUOTA_BACKOFF_BASE_SECONDS * (2**attempt)
+ logger.warning(
+ "GSC request connection error, backing off",
+ site_url=site_url,
+ attempt=attempt,
+ wait_seconds=wait,
+ )
+ time.sleep(wait)
+ continue
+
if response.ok:
return response.json().get("rows", [])
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 4596624629e0..4894e76494c4 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
@@ -512,6 +512,38 @@ def test_query_server_error_bubbles_http_error_after_max_retries(monkeypatch):
assert session.post.call_count == QUOTA_MAX_RETRIES + 1
+def test_query_retries_connection_error_then_succeeds(monkeypatch):
+ monkeypatch.setattr(gsc.time, "sleep", lambda _s: None)
+ monkeypatch.setattr(gsc, "_throttle", lambda _site: None)
+
+ session = mock.MagicMock()
+ session.post.side_effect = [
+ requests.ConnectionError("Connection aborted."),
+ requests.ConnectionError("Connection aborted."),
+ _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 == 3
+
+
+def test_query_connection_error_bubbles_after_max_retries(monkeypatch):
+ monkeypatch.setattr(gsc.time, "sleep", lambda _s: None)
+ monkeypatch.setattr(gsc, "_throttle", lambda _site: None)
+
+ session = mock.MagicMock()
+ session.post.side_effect = requests.ConnectionError("Connection aborted.")
+
+ # A persistent connection reset exhausts the inline budget and surfaces the real
+ # ConnectionError (retryable at the activity level).
+ with pytest.raises(requests.ConnectionError):
+ _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_throttle_spaces_requests_per_site(monkeypatch):
gsc._next_request_at.clear()
fake_now = {"t": 100.0}