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_sheets): retry read timeouts and connection resets inline (#68718)

Tom Owersf5e6d82371d3
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.py
index 4309b85e50b8..9c161a948f77 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.py
@@ -6,6 +6,7 @@ from typing import Any, Optional, TypeVar
from django.conf import settings
import gspread
+import requests
from cachetools import Cache, TTLCache, cached
from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account
@@ -119,8 +120,14 @@ def _retry_on_transient_api_error(execute: Callable[[], T]) -> T:
while True:
try:
return execute()
- except gspread.exceptions.APIError as e:
- if not _is_retryable_api_error(e) or attempts >= max_attempts:
+ except (gspread.exceptions.APIError, requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
+ # A dropped connection or read timeout is raised by `requests` before gspread can wrap
+ # it in an APIError, so the status-code check never sees it, and the tracked adapter's
+ # transport retries are already spent by the time it reaches us. It's a transient
+ # network blip, so retry inline like a 5xx; APIErrors still gate on their status. Once
+ # the budget is spent, let it bubble so Temporal retries the activity.
+ is_retryable = _is_retryable_api_error(e) if isinstance(e, gspread.exceptions.APIError) else True
+ if not is_retryable or attempts >= max_attempts:
raise
jitter = random.uniform(-jitter_in_seconds, jitter_in_seconds)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
index 11ca5841169e..5aa6cc4565a9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
@@ -5,6 +5,7 @@ import pytest
from unittest import mock
import gspread
+import requests
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs import GoogleSheetsSourceConfig
from products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.google_sheets import (
@@ -319,6 +320,48 @@ def test_retry_on_transient_api_error_does_not_retry_non_transient():
assert fn.call_count == 1
+ "error",
+ [
+ requests.exceptions.ConnectionError("Connection aborted."),
+ requests.exceptions.ReadTimeout("Read timed out. (read timeout=120.0)"),
+ requests.exceptions.ConnectTimeout("Connection timed out."),
+ ],
+)
+def test_retry_on_transient_api_error_retries_network_error_then_succeeds(error):
+ """A dropped connection or read timeout is raised by `requests` before gspread wraps it in an
+ APIError, so the status-code path never sees it. It's a transient blip and must be retried
+ inline rather than failing the read on the first occurrence."""
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.google_sheets.time"
+ ):
+ fn = mock.MagicMock(side_effect=[error, error, "ok"])
+
+ assert _retry_on_transient_api_error(fn) == "ok"
+ assert fn.call_count == 3
+
+
+ "error",
+ [
+ requests.exceptions.ConnectionError("Connection aborted."),
+ requests.exceptions.ReadTimeout("Read timed out. (read timeout=120.0)"),
+ ],
+)
+def test_retry_on_transient_api_error_bubbles_network_error_after_max_retries(error):
+ """A persistent network error exhausts the inline budget and re-raises so it stays retryable
+ at the activity level (Temporal), rather than being swallowed."""
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.google_sheets.time"
+ ):
+ fn = mock.MagicMock(side_effect=error)
+
+ with pytest.raises(type(error)):
+ _retry_on_transient_api_error(fn)
+
+ assert fn.call_count == 10
+
+
def test_google_sheets_source_retries_transient_error_on_data_reads():
"""A transient 5xx on the cell-reading calls (`get_all_values`/`get_all_records`)
must be retried, not surfaced on the first occurrence. These reads issue their own