: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_sheets): bound Sheets API request timeout (#64069)
Tom Owerscdcbaad29525
diff --git a/posthog/temporal/data_imports/sources/google_sheets/google_sheets.py b/posthog/temporal/data_imports/sources/google_sheets/google_sheets.pyindex 19b1de7e5a5e..f8e79f11e4c4 100644--- a/posthog/temporal/data_imports/sources/google_sheets/google_sheets.py+++ b/posthog/temporal/data_imports/sources/google_sheets/google_sheets.py@@ -18,6 +18,15 @@ from posthog.temporal.data_imports.sources.generated_configs import GoogleSheetsfrom products.data_warehouse.backend.types import IncrementalField, IncrementalFieldType+# (connect, read) timeout for every Sheets API request, in seconds. gspread defaults to no+# timeout, so a stalled connection blocks the worker thread indefinitely. Because the sync+# activities are threaded, a blocked thread can't be interrupted cleanly — Temporal eventually+# hits the activity's `start_to_close_timeout` and raises a `CancelledError` into the thread mid+# socket-read, which surfaces as a noisy "Cancelled" error. A bounded timeout turns a stall into a+# fast, retryable `requests.Timeout` instead. The read timeout is the max gap between received+# bytes (not total download time), so it stays safe for large sheets that stream in steadily.+_REQUEST_TIMEOUT_SECONDS: tuple[float, float] = (30.0, 120.0)+def google_sheets_client() -> gspread.Client:credentials = service_account.Credentials.from_service_account_info(@@ -36,7 +45,9 @@ def google_sheets_client() -> gspread.Client:adapter = make_tracked_adapter()session.mount("https://", adapter)session.mount("http://", adapter)- return gspread.authorize(credentials, session=session)+ client = gspread.authorize(credentials, session=session)+ client.set_timeout(_REQUEST_TIMEOUT_SECONDS)+ return clientcache: Cache[Any, Any] = TTLCache(maxsize=500, ttl=120) # 120 secondsdiff --git a/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.pyindex ad92be08114b..7c4b2accf6bd 100644--- a/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py+++ b/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py@@ -9,10 +9,12 @@ import gspreadfrom posthog.temporal.data_imports.sources.generated_configs import GoogleSheetsSourceConfigfrom posthog.temporal.data_imports.sources.google_sheets.google_sheets import (_PERMISSION_DENIED_MESSAGE,+ _REQUEST_TIMEOUT_SECONDS,_get_worksheet,_retry_on_transient_api_error,get_schema_incremental_fields,get_schemas,+ google_sheets_client,google_sheets_source,)from posthog.temporal.data_imports.sources.google_sheets.source import GoogleSheetsSource@@ -379,3 +381,19 @@ def test_get_schema_incremental_fields_reraises_other_api_errors():get_schema_incremental_fields(config, "sheet1")assert mock_worksheet.get_all_values.call_count == 10+++def test_google_sheets_client_sets_request_timeout():+ """Every gspread client must be built with a bounded (connect, read) timeout. gspread defaults+ to no timeout, so a stalled Sheets API read blocks the threaded sync activity until Temporal's+ start_to_close_timeout cancels the thread mid-read — which surfaces as a noisy CancelledError+ rather than a fast, retryable timeout."""+ with (+ mock.patch("posthog.temporal.data_imports.sources.google_sheets.google_sheets.service_account"),+ mock.patch("posthog.temporal.data_imports.sources.google_sheets.google_sheets.AuthorizedSession"),+ mock.patch("posthog.temporal.data_imports.sources.google_sheets.google_sheets.make_tracked_adapter"),+ ):+ client = google_sheets_client()++ assert client.http_client.timeout == _REQUEST_TIMEOUT_SECONDS+ assert client.http_client.timeout is not None