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(google_ads): retry a gRPC "Stream removed" as transient (#109285)

Tom Owers84c7d921c46e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py
index edec5a4bbdca..b97805dd399e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py
@@ -787,6 +787,15 @@ _RECEIVE_LIMIT_EXHAUSTED_SIGNATURE = "Received message larger than max"
# ``UNKNOWN`` status, which covers far too broad a range of unrelated failures to retry blindly.
_AUTH_BACKEND_UNKNOWN_ERROR_SIGNATURE = "Authentication backend unknown error"
+# gRPC's Python client surfaces a peer-initiated HTTP/2 stream reset (a load balancer recycling the
+# underlying connection, a momentary backend restart) as a bare ``UNKNOWN`` status with this detail
+# string rather than a code that already maps to a transient status — a long-documented, widely
+# reported gRPC behavior, not an application-level failure. The request itself was never processed,
+# so a retry on a fresh stream is safe and typically succeeds. Matched on this specific message for
+# the same reason as the auth-backend signature above: the bare ``UNKNOWN`` status alone is too broad
+# a signal to retry blindly.
+_STREAM_REMOVED_ERROR_SIGNATURE = "Stream removed"
+
def _is_transient_grpc_error(exc: BaseException) -> bool:
"""Return True for a transient gRPC failure Google's guidance says to retry.
@@ -796,12 +805,15 @@ def _is_transient_grpc_error(exc: BaseException) -> bool:
(whose ``code()`` returns the ``StatusCode``) can also propagate. The Google Ads SDK additionally
re-wraps the transport error in a ``GoogleAdsException`` when it can pull an ads ``failure`` from
the trailing metadata (e.g. a backend ``DEADLINE_EXCEEDED`` returned alongside the status); the
- gRPC status then lives on the wrapped ``error``, so we unwrap and inspect it too.
+ gRPC status then lives on the wrapped ``error``, so we unwrap and inspect it too. A bare
+ ``UNKNOWN`` status is only treated as transient for the specific messages known to be benign
+ (see ``_AUTH_BACKEND_UNKNOWN_ERROR_SIGNATURE`` and ``_STREAM_REMOVED_ERROR_SIGNATURE``).
"""
if isinstance(exc, google_api_exceptions.ServiceUnavailable | google_api_exceptions.InternalServerError):
return True
if isinstance(exc, google_api_exceptions.Unknown):
- return _AUTH_BACKEND_UNKNOWN_ERROR_SIGNATURE in str(exc)
+ message = str(exc)
+ return _AUTH_BACKEND_UNKNOWN_ERROR_SIGNATURE in message or _STREAM_REMOVED_ERROR_SIGNATURE in message
candidate: typing.Any = exc.error if isinstance(exc, GoogleAdsException) else exc
# ``ResourceExhausted`` exposes ``code`` as an HTTP int, not a callable ``StatusCode``, so the
# gapic-wrapped form is matched by type rather than via the ``code()`` check below.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
index 9375e2c78821..7f6fca01495c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
@@ -983,6 +983,10 @@ class TestTransientGrpcErrorDetection:
# A bare UNKNOWN status carrying Google's own auth-backend hiccup message is a confirmed
# transient backend incident, not a rejected credential — ride it out in-process.
(google_api_exceptions.Unknown("Authentication backend unknown error."), True),
+ # A bare UNKNOWN status carrying "Stream removed" is a peer-initiated HTTP/2 stream reset
+ # (e.g. a load balancer recycling the connection), not an application failure — ride it
+ # out in-process the same way.
+ (google_api_exceptions.Unknown("Stream removed"), True),
# Any other UNKNOWN-status error must not be retried blindly — the status alone is too
# broad a signal, so only the specific known message is treated as transient.
(google_api_exceptions.Unknown("Some other unrelated backend failure."), False),