: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): retry a 409 conflict from the Sheets API instead of failing the sync (#88916)
Tom Owers345e9542f9c8
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.pyindex 1ae7e6cdf81d..d6e1685203cc 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@@ -70,11 +70,13 @@ max_attempts = 10jitter_in_seconds = 10sleep_per_attempt_in_seconds = 30-# Transient Google Sheets API failures worth retrying: 429 (per-minute quota exhausted) plus the-# 5xx server-side errors Google returns intermittently (e.g. "[500]: Internal error encountered.").-# Google's API guidance is to retry these with backoff — they are not caused by our request and-# usually clear on the next attempt, so re-raising immediately turns a blip into a failed sync.-_RETRYABLE_API_ERROR_CODES = {429, 500, 502, 503, 504}+# Transient Google Sheets API failures worth retrying: 429 (per-minute quota exhausted), the+# 5xx server-side errors Google returns intermittently (e.g. "[500]: Internal error encountered."),+# and 409 ("[409]: The operation was aborted.") — Google's ABORTED code, a concurrency conflict+# (e.g. another process editing the same spreadsheet) rather than a problem with our request.+# Google's API guidance is to retry these with backoff — they usually clear on the next attempt,+# so re-raising immediately turns a blip into a failed sync.+_RETRYABLE_API_ERROR_CODES = {409, 429, 500, 502, 503, 504}# gspread raises a bare `PermissionError` (with no message) when the Google Sheets# API returns 403 — see gspread/client.py: `raise PermissionError from ex`.diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.pyindex 7fbbcc49e30b..cc27022d34a2 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py@@ -80,6 +80,7 @@ class GoogleSheetsSource(SimpleSource[GoogleSheetsSourceConfig]):# reword. Temporal then retries the whole activity, so the failure is transient and# self-recovering.return {+ "APIError: [409]","APIError: [429]","APIError: [500]","APIError: [502]",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.pyindex 38a9dc2d72ba..4020ae1e3224 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@@ -66,7 +66,7 @@ def test_get_worksheet_backoff():assert mock_get_worksheet_by_id.call_count == 10[email protected]("status_code", [429, 500, 502, 503, 504])[email protected]("status_code", [409, 429, 500, 502, 503, 504])def test_get_worksheet_retries_transient_api_errors(status_code):"""Transient API errors (quota 429s and 5xx server errors like"[500]: Internal error encountered.") should be retried with backoff, not@@ -102,7 +102,7 @@ def test_get_worksheet_retries_transient_api_errors(status_code):assert mock_get_worksheet_by_id.call_count == 10[email protected]("status_code", [429, 500, 502, 503, 504])[email protected]("status_code", [409, 429, 500, 502, 503, 504])def test_get_schemas_retries_transient_api_errors(status_code):"""Schema discovery (`open_by_url`/`worksheets`) hits the Sheets API just like`_get_worksheet`, so a transient quota 429 or 5xx server error (e.g.@@ -231,10 +231,11 @@ def test_reraises_permission_error_with_message(call_site):assert "Spreadsheet access denied" in str(exc_info.value)[email protected]("status_code", [429, 500, 502, 503, 504])[email protected]("status_code", [409, 429, 500, 502, 503, 504])def test_retry_on_transient_api_error_retries_then_exhausts(status_code):- """The shared retry helper must retry transient API errors (quota 429s and 5xx- server errors like "[503]: The service is currently unavailable.") with backoff."""+ """The shared retry helper must retry transient API errors (quota 429s, 5xx+ server errors like "[503]: The service is currently unavailable.", and 409+ concurrency conflicts) with backoff."""with mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.google_sheets.time"):@@ -530,7 +531,7 @@ def test_error_string_matches_a_non_retryable_key(error):assert any(key in str(error) for key in non_retryable_errors)[email protected]("status_code", [429, 500, 502, 503, 504])[email protected]("status_code", [409, 429, 500, 502, 503, 504])def test_error_string_matches_a_retryable_key(status_code):"""`_retry_on_transient_api_error` already retries these status codes in-process beforere-raising; the framework then logs at `warning` instead of `exception` only if the re-raised