: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(google_search_console): retry token-refresh transport errors inline (#99642)
Tom Owers7bd253dad0a8
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.pyindex d2cca254f2cf..37da36bc4944 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@@ -10,7 +10,7 @@ from django.db import OperationalError, close_old_connectionsimport requestsimport structlog-from google.auth.exceptions import RefreshError+from google.auth.exceptions import RefreshError, TransportErrorfrom google.auth.transport.requests import AuthorizedSessionfrom google.oauth2.credentials import Credentials as OAuthCredentials@@ -379,6 +379,24 @@ def _query_search_analytics()time.sleep(wait)continue+ except TransportError:+ # Raised by AuthorizedSession's internal token-refresh request when the underlying+ # HTTP call itself fails (connection reset, proxy error, DNS failure, timeout) before+ # any response exists — google-auth wraps `requests.RequestException` in this class+ # rather than raising it directly, so it never reaches the ConnectionError/Timeout+ # handling above. Always a network-layer failure, same transient class, so retry+ # inline like a 5xx rather than crashing the activity on the first blip.+ if attempt == QUOTA_MAX_RETRIES:+ raise+ wait = QUOTA_BACKOFF_BASE_SECONDS * (2**attempt)+ logger.warning(+ "GSC token refresh transport error, backing off",+ site_url=site_url,+ attempt=attempt,+ wait_seconds=wait,+ )+ time.sleep(wait)+ continueif response.ok:try: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.pyindex ee4cf6df7cea..28d8f6e7f26b 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@@ -6,7 +6,7 @@ from unittest import mockfrom django.db import OperationalErrorimport requests-from google.auth.exceptions import RefreshError+from google.auth.exceptions import RefreshError, TransportErrorfrom products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.googlesearchconsole import (GoogleSearchConsoleSourceConfig,@@ -947,6 +947,39 @@ def test_query_permanent_token_refresh_error_bubbles_without_retry(monkeypatch):assert session.post.call_count == 1+def test_query_retries_token_refresh_transport_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 = [+ # AuthorizedSession wraps a network-layer failure (e.g. a proxy error) hit while+ # refreshing the access token in this class, not RefreshError.+ TransportError("Cannot connect to proxy."),+ _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_token_refresh_transport_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 = TransportError("Cannot connect to proxy.")++ # A persistent token-refresh transport failure exhausts the inline budget and surfaces the+ # real TransportError (retryable at the activity level).+ with pytest.raises(TransportError):+ _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}