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): retry create_read_session on transient internal errors (#79071)

Tom Owers38ec805e29f4
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 06d40cabc783..ba2dcf0a6d05 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
@@ -25,7 +25,14 @@ from urllib.parse import urlparse
import pyarrow as pa
import structlog
-from google.api_core.exceptions import BadRequest, Forbidden, InternalServerError, NotFound, ServiceUnavailable
+from google.api_core.exceptions import (
+ BadRequest,
+ DeadlineExceeded,
+ Forbidden,
+ InternalServerError,
+ NotFound,
+ ServiceUnavailable,
+)
from google.api_core.retry import Retry, if_exception_type
from google.auth.exceptions import RefreshError
from google.auth.transport.requests import AuthorizedSession
@@ -259,6 +266,21 @@ BIGQUERY_READ_ROWS_RETRY = Retry(
)
+# The same transient gRPC INTERNAL error can surface one call earlier, from `create_read_session`
+# itself, before any stream exists to reconnect. The client's default create_read_session retry
+# only covers DeadlineExceeded/ServiceUnavailable, so INTERNAL escapes as an unhandled
+# InternalServerError and fails the whole import activity. No stream has been read yet at this
+# point, so retrying just creates a fresh session. Parameters mirror the library's default
+# create_read_session retry, widened to also retry INTERNAL.
+BIGQUERY_CREATE_READ_SESSION_RETRY = Retry(
+ predicate=if_exception_type(DeadlineExceeded, ServiceUnavailable, InternalServerError),
+ initial=0.1,
+ maximum=60.0,
+ multiplier=1.3,
+ deadline=600.0,
+)
+
+
class BigQueryDatasetNotFoundError(Exception):
"""Raised when schema discovery queries a dataset/table that doesn't exist in the queried region.
@@ -1573,6 +1595,7 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
read_session=requested_session,
# TODO: Currently, single stream. Could multi-thread here for performance.
max_stream_count=1,
+ retry=BIGQUERY_CREATE_READ_SESSION_RETRY,
)
if not read_session.streams:
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 57a39498f5f2..f0c66ce2aeb7 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
@@ -7,6 +7,7 @@ from unittest import mock
from dateutil import parser
from google.api_core.exceptions import (
BadRequest,
+ DeadlineExceeded,
Forbidden,
InternalServerError,
InvalidArgument,
@@ -18,6 +19,7 @@ from google.auth.exceptions import RefreshError
from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery import bigquery as bq_module
from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery import (
+ BIGQUERY_CREATE_READ_SESSION_RETRY,
BIGQUERY_CREDENTIALS_REJECTED_ERROR,
BIGQUERY_DATASET_NOT_FOUND_ERROR,
BIGQUERY_INVALID_IDENTIFIER_ERROR,
@@ -1561,6 +1563,33 @@ def test_bigquery_read_rows_retry_does_not_reconnect_on_deterministic_errors(exc
assert BIGQUERY_READ_ROWS_RETRY._predicate(exc) is False
+ "exc",
+ [
+ # The observed production failure: `create_read_session` itself returning a transient
+ # gRPC INTERNAL before any stream exists. The library default only retries
+ # DeadlineExceeded/ServiceUnavailable, so this escaped and crashed the import activity.
+ InternalServerError("request failed: internal error"),
+ ServiceUnavailable("503 The service is currently unavailable."),
+ DeadlineExceeded("504 Deadline Exceeded"),
+ ],
+)
+def test_bigquery_create_read_session_retry_retries_transient_errors(exc):
+ assert BIGQUERY_CREATE_READ_SESSION_RETRY._predicate(exc) is True
+
+
+ "exc",
+ [
+ # Deterministic failures must surface rather than retry forever.
+ NotFound("404 Requested table was not found."),
+ BadRequest("400 request failed"),
+ ],
+)
+def test_bigquery_create_read_session_retry_does_not_retry_deterministic_errors(exc):
+ assert BIGQUERY_CREATE_READ_SESSION_RETRY._predicate(exc) is False
+
+
def test_bigquery_get_primary_keys_for_table_passes_job_retry():
"""The primary-key probe must run under the extended job retry so a transient BigQuery job
error is re-tried in place instead of crashing the import."""