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 transient UNAVAILABLE wrapped in GoogleAdsException (#65956)

Tom Owerse31b05cf06b3
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 0bcd0bd2b5e0..9fc53d2869dd 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
@@ -491,11 +491,15 @@ def _is_transient_grpc_unavailable(exc: BaseException) -> bool:
The gapic transport usually surfaces it as ``google.api_core.exceptions.ServiceUnavailable``,
but the raw ``grpc`` ``_InactiveRpcError`` (whose ``code()`` returns ``StatusCode.UNAVAILABLE``)
- can also propagate, so we accept either shape.
+ 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 an ``UNAVAILABLE`` status); the gRPC status
+ then lives on the wrapped ``error``, so we unwrap and inspect it too.
"""
if isinstance(exc, google_api_exceptions.ServiceUnavailable):
return True
- code = getattr(exc, "code", None)
+ candidate: typing.Any = exc.error if isinstance(exc, GoogleAdsException) else exc
+ code = getattr(candidate, "code", None)
return callable(code) and code() == grpc.StatusCode.UNAVAILABLE
@@ -508,7 +512,8 @@ def _search_with_transient_retry(
"""Call ``GoogleAdsService.search``, retrying a transient gRPC ``UNAVAILABLE`` with backoff.
Only retries before any page is yielded or checkpointed, so there is no partial state to
- reconcile. Non-transient errors (including ``GoogleAdsException``) re-raise immediately so the
+ reconcile. The transient ``UNAVAILABLE`` may itself arrive wrapped in a ``GoogleAdsException``
+ (see ``_is_transient_grpc_unavailable``). Non-transient errors re-raise immediately so the
caller's ``INVALID_PAGE_TOKEN`` handling and Temporal's retry policy still apply.
"""
attempt = 0
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 a3e0d1b9d371..dbb62928c922 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
@@ -547,16 +547,26 @@ def _grpc_unavailable_error() -> grpc.RpcError:
return _UnavailableRpcError()
+def _google_ads_exception_wrapping(grpc_error: grpc.RpcError) -> GoogleAdsException:
+ """A ``GoogleAdsException`` carrying a transport-level error on ``error``, the shape the SDK
+ raises when it can pull an ads ``failure`` from the trailing metadata alongside the gRPC status.
+ """
+ return GoogleAdsException(error=grpc_error, call=None, failure=None, request_id="req-1")
+
+
class TestTransientGrpcUnavailableDetection:
@pytest.mark.parametrize(
"exc, expected",
[
(google_api_exceptions.ServiceUnavailable("502:Bad Gateway"), True),
(_grpc_unavailable_error(), True),
+ # The SDK wraps the transport UNAVAILABLE in a GoogleAdsException when an ads failure
+ # rides along (e.g. a backend DEADLINE_EXCEEDED) — still transient, so we unwrap it.
+ (_google_ads_exception_wrapping(_grpc_unavailable_error()), True),
# A different gapic error must not be treated as transient-unavailable.
(google_api_exceptions.PermissionDenied("PERMISSION_DENIED"), False),
- # Google Ads API errors carry no UNAVAILABLE gRPC status — they route through
- # the existing GoogleAdsException handling, not the transient retry.
+ # An ads-API GoogleAdsException carries no UNAVAILABLE gRPC status — it routes through
+ # the existing INVALID_PAGE_TOKEN handling, not the transient retry.
(_google_ads_exception(RequestErrorEnum.RequestError.INVALID_PAGE_TOKEN), False),
(ValueError("boom"), False),
],
@@ -587,6 +597,7 @@ class TestSearchTransientRetry:
[
google_api_exceptions.ServiceUnavailable("502:Bad Gateway"),
_grpc_unavailable_error(),
+ _google_ads_exception_wrapping(_grpc_unavailable_error()),
],
)
def test_rides_out_transient_unavailable(self, error):