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): reconnect storage reads on transient RST_STREAM (#68482)

Tom Owers45e527371662
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 2bbe2ab0975f..6afe4348169c 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,8 @@ from urllib.parse import urlparse
import pyarrow as pa
import structlog
-from google.api_core.exceptions import BadRequest, Forbidden, NotFound
+from google.api_core.exceptions import BadRequest, 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
from google.cloud import bigquery, bigquery_storage
@@ -144,6 +145,21 @@ def _query_job_should_retry(exc: Exception) -> bool:
BIGQUERY_QUERY_JOB_RETRY = DEFAULT_JOB_RETRY.with_predicate(_query_job_should_retry)
+# The Storage Read API can drop a ReadRows stream mid-flight with a transient gRPC INTERNAL error
+# ("Received RST_STREAM with error code 2"). The client's default ReadRows retry only reconnects on
+# ServiceUnavailable, so the INTERNAL escapes as an unhandled InternalServerError and fails the whole
+# read, and with it the import activity, which then re-runs the (potentially large) temp-table copy
+# from scratch. Reconnecting resumes the stream from the last-read offset, so widen the default
+# predicate to also retry INTERNAL. Parameters mirror the library's default ReadRows retry.
+BIGQUERY_READ_ROWS_RETRY = Retry(
+ predicate=if_exception_type(ServiceUnavailable, InternalServerError),
+ initial=0.1,
+ maximum=60.0,
+ multiplier=1.3,
+ deadline=86400.0,
+)
+
+
class BigQueryDatasetNotFoundError(Exception):
"""Raised when schema discovery queries a dataset/table that doesn't exist in the queried region.
@@ -1357,7 +1373,7 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
return
stream_name = read_session.streams[0].name
- read_rows_stream = bq_storage.read_rows(stream_name)
+ read_rows_stream = bq_storage.read_rows(stream_name, retry=BIGQUERY_READ_ROWS_RETRY)
rows_iterator = read_rows_stream.rows()
record_batches = []
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 de27164c51ff..3c569f965c84 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
@@ -5,7 +5,15 @@ from freezegun import freeze_time
from unittest import mock
from dateutil import parser
-from google.api_core.exceptions import BadRequest, Forbidden, InvalidArgument, NotFound, PermissionDenied
+from google.api_core.exceptions import (
+ BadRequest,
+ Forbidden,
+ InternalServerError,
+ InvalidArgument,
+ NotFound,
+ PermissionDenied,
+ ServiceUnavailable,
+)
from google.auth.exceptions import RefreshError
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.typings import SourceInputs
@@ -14,6 +22,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.b
BIGQUERY_DATASET_NOT_FOUND_ERROR,
BIGQUERY_INVALID_IDENTIFIER_ERROR,
BIGQUERY_QUERY_JOB_RETRY,
+ BIGQUERY_READ_ROWS_RETRY,
BIGQUERY_TOKEN_RESPONSE_ERROR,
BigQueryCredentialsRejectedError,
BigQueryDatasetNotFoundError,
@@ -1140,6 +1149,31 @@ def test_bigquery_query_job_retry_does_not_retry_deterministic_errors(exc):
assert BIGQUERY_QUERY_JOB_RETRY._predicate(exc) is False
+ "exc",
+ [
+ # The observed transient: a dropped Storage Read stream surfaced as a gRPC INTERNAL. The
+ # library default only reconnects on ServiceUnavailable, so this escaped and crashed the read.
+ InternalServerError("Received RST_STREAM with error code 2"),
+ ServiceUnavailable("503 The service is currently unavailable."),
+ ],
+)
+def test_bigquery_read_rows_retry_reconnects_on_transient_stream_errors(exc):
+ assert BIGQUERY_READ_ROWS_RETRY._predicate(exc) is True
+
+
+ "exc",
+ [
+ # Deterministic failures must surface rather than reconnect forever.
+ NotFound("404 Requested stream was not found."),
+ BadRequest("400 request failed"),
+ ],
+)
+def test_bigquery_read_rows_retry_does_not_reconnect_on_deterministic_errors(exc):
+ assert BIGQUERY_READ_ROWS_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."""