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(bigquery): don't fail a completed sync when temp-table cleanup errors (#73815)

Tom Owersf463bf868ad2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
index 0f74607ed666..05ca38fb3e62 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
@@ -1352,17 +1352,32 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
rest_api_version=_bigquery_rest_api_version(inputs.api_version),
)
finally:
- # Delete the destination table (if it exists) after we're done with it
- delete_table(
- table_id=destination_table,
- project_id=project_id,
- location=region,
- private_key=config.key_file.private_key,
- private_key_id=config.key_file.private_key_id,
- client_email=config.key_file.client_email,
- token_uri=config.key_file.token_uri,
- )
- inputs.logger.info(f"Deleting bigquery temp destination table: {destination_table}")
+ # Delete the destination table (if it exists) after we're done with it. A transient
+ # token-refresh failure here (e.g. a 502 from Google's OAuth endpoint) must not turn
+ # an otherwise-successful sync into a failure — retrying the whole sync just to retry
+ # this delete is wasteful. This must NOT swallow a genuine permission denial: this
+ # table holds a real materialized copy of the customer's data, so if we can't delete
+ # it we need the sync to keep failing (via the "Access Denied:" key in
+ # `get_non_retryable_errors`) rather than silently leaving readable copies to
+ # accumulate on every run.
+ try:
+ delete_table(
+ table_id=destination_table,
+ project_id=project_id,
+ location=region,
+ private_key=config.key_file.private_key,
+ private_key_id=config.key_file.private_key_id,
+ client_email=config.key_file.client_email,
+ token_uri=config.key_file.token_uri,
+ )
+ inputs.logger.info(f"Deleting bigquery temp destination table: {destination_table}")
+ except RefreshError as e:
+ # `invalid_grant` (rejected credentials) is not transient — `_build_source_response`
+ # authenticates with the same credentials, so genuinely dead credentials need to keep
+ # propagating to the sync-path classifier rather than being silently swallowed here.
+ if "invalid_grant" in str(e):
+ raise
+ inputs.logger.warning(f"Skipping cleanup of bigquery destination table {destination_table}: {e}")
def _build_source_response(
self,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
index ed43b216f63f..d41167b8c6f1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
@@ -304,6 +304,67 @@ def test_bigquery_build_pipeline_resolves_dataset_routing(
assert mock_delete.call_args.kwargs["table_id"] == expected_table_id
+def test_bigquery_build_pipeline_swallows_transient_cleanup_refresh_error():
+ """A transient token-refresh failure (e.g. a 502 from Google's OAuth endpoint) while deleting
+ the run's own destination table must not turn an otherwise-successful sync into a failure —
+ retrying the whole sync just to retry this delete is wasteful."""
+ config = _make_config()
+ logger = mock.MagicMock()
+ inputs = _make_inputs(logger=logger)
+ build_result = mock.MagicMock()
+
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery.delete_all_temp_destination_tables",
+ ),
+ mock.patch.object(BigQueryImplementation, "_build_source_response", return_value=build_result),
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery.delete_table",
+ side_effect=RefreshError(
+ "<!DOCTYPE html><html><head><title>Error 502 (Server Error)</title></head></html>"
+ ),
+ ),
+ ):
+ result = BigQuerySource().source_for_pipeline(config, inputs)
+
+ assert result is build_result
+ logger.warning.assert_called_once()
+
+
+ "exception",
+ [
+ # A genuine permission denial must keep propagating: this table holds a real materialized
+ # copy of the customer's data, so swallowing it here would let readable copies accumulate
+ # on every run instead of the sync failing via the "Access Denied:" non-retryable key.
+ Forbidden("Access Denied: Permission bigquery.tables.delete denied on table"),
+ # `invalid_grant` (rejected credentials) is not transient and must reach the sync-path
+ # classifier rather than being silently swallowed as a routine refresh hiccup.
+ RefreshError(("invalid_grant: Invalid JWT Signature.", {"error": "invalid_grant"})),
+ RuntimeError("boom"),
+ ],
+)
+def test_bigquery_build_pipeline_propagates_unexpected_cleanup_errors(exception):
+ """Only a transient (non-`invalid_grant`) `RefreshError` is treated as best-effort during
+ destination-table cleanup — anything else, including permission denials and rejected
+ credentials, must still surface."""
+ config = _make_config()
+ inputs = _make_inputs()
+
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery.delete_all_temp_destination_tables",
+ ),
+ mock.patch.object(BigQueryImplementation, "_build_source_response", return_value=mock.MagicMock()),
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery.delete_table",
+ side_effect=exception,
+ ),
+ pytest.raises(type(exception)),
+ ):
+ BigQuerySource().source_for_pipeline(config, inputs)
+
+
@pytest.mark.parametrize(
"enabled_columns,primary_keys,incremental_field,expected",
[