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(temporalio): retry transient gRPC deadline timeouts in-process (#66470)

Tom Owers1616598cc8d2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py
index 1ef7af12adad..7333a4bfae09 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py
@@ -55,19 +55,22 @@ class TemporalIOResumeConfig:
T = TypeVar("T")
-# Temporal Cloud throttles per-namespace RPCs (RPS / concurrency / overload) with a gRPC
-# RESOURCE_EXHAUSTED status — e.g. "namespace rate limit exceeded". It's transient: a short backoff
-# usually clears the window. Riding it out in-process keeps a brief throttle from failing the whole
-# import activity (which would rebuild the client and restart pagination) and avoids error-tracking
-# noise. Persistent throttling re-raises so Temporal's activity retry still applies.
-_MAX_RATE_LIMIT_ATTEMPTS = 6
+# Temporal Cloud surfaces transient server-side conditions as gRPC errors that a short backoff
+# usually clears: per-namespace throttling (RESOURCE_EXHAUSTED — e.g. "namespace rate limit
+# exceeded") and request deadlines on the visibility/history services (DEADLINE_EXCEEDED — e.g.
+# "downstream duration timeout"). Riding these out in-process keeps a brief blip from failing the
+# whole import activity (which would rebuild the client and restart pagination) and avoids
+# error-tracking noise. Persistent failures re-raise so Temporal's activity retry still applies.
+_MAX_TRANSIENT_RPC_ATTEMPTS = 6
+_RETRYABLE_RPC_STATUSES = frozenset({RPCStatusCode.RESOURCE_EXHAUSTED, RPCStatusCode.DEADLINE_EXCEEDED})
-async def _with_rate_limit_retry(
+
+async def _with_transient_rpc_retry(
operation: Callable[[], Awaitable[T]],
logger: FilteringBoundLogger,
*,
- max_attempts: int = _MAX_RATE_LIMIT_ATTEMPTS,
+ max_attempts: int = _MAX_TRANSIENT_RPC_ATTEMPTS,
) -> T:
attempt = 0
while True:
@@ -75,10 +78,15 @@ async def _with_rate_limit_retry(
return await operation()
except RPCError as e:
attempt += 1
- if attempt >= max_attempts or e.status != RPCStatusCode.RESOURCE_EXHAUSTED:
+ if attempt >= max_attempts or e.status not in _RETRYABLE_RPC_STATUSES:
raise
backoff = min(2 * attempt, 30)
- logger.debug("TemporalIO: namespace rate limited", backoff_seconds=backoff, attempt=attempt)
+ logger.debug(
+ "TemporalIO: transient RPC error, backing off",
+ status=e.status,
+ backoff_seconds=backoff,
+ attempt=attempt,
+ )
await asyncio.sleep(backoff)
@@ -206,7 +214,7 @@ async def _get_workflows(
# Save the token that will be used to fetch this page *before* fetching.
# On resume we re-fetch this same page — duplicates are safe thanks to primary keys.
pre_fetch_token = workflows.next_page_token
- await _with_rate_limit_retry(workflows.fetch_next_page, logger)
+ await _with_transient_rpc_retry(workflows.fetch_next_page, logger)
page = workflows.current_page
if not page:
break
@@ -257,7 +265,7 @@ async def _get_workflow_histories(
# Save the token that will be used to fetch this page *before* fetching.
# On resume we re-fetch this same page — duplicates are safe thanks to primary keys.
pre_fetch_token = workflows.next_page_token
- await _with_rate_limit_retry(workflows.fetch_next_page, logger)
+ await _with_transient_rpc_retry(workflows.fetch_next_page, logger)
page = workflows.current_page
if not page:
break
@@ -274,7 +282,7 @@ async def _get_workflow_histories(
for item in page:
try:
handle = client.get_workflow_handle(item.id, run_id=item.run_id)
- history = await _with_rate_limit_retry(handle.fetch_history, logger)
+ history = await _with_transient_rpc_retry(handle.fetch_history, logger)
history_dict = history.to_json_dict()
events = history_dict["events"]
for event in events:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py
index b03c15a52a5b..bfc91cea0b83 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py
@@ -11,7 +11,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.temporalio
from products.warehouse_sources.backend.temporal.data_imports.sources.temporalio.temporalio import (
FakeSettings,
_get_temporal_client,
- _with_rate_limit_retry,
+ _with_transient_rpc_retry,
)
@@ -105,37 +105,51 @@ class TestTemporalIONonRetryableErrors:
)
-class TestRateLimitRetry:
+class TestTransientRPCRetry:
+ @pytest.mark.parametrize(
+ "message,status",
+ [
+ ("namespace rate limit exceeded", RPCStatusCode.RESOURCE_EXHAUSTED),
+ ("downstream duration timeout", RPCStatusCode.DEADLINE_EXCEEDED),
+ ],
+ )
@patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.temporalio.temporalio.asyncio.sleep",
new_callable=AsyncMock,
)
- async def test_rides_out_transient_rate_limit(self, sleep):
+ async def test_rides_out_transient_error(self, sleep, message, status):
calls = {"n": 0}
async def operation():
calls["n"] += 1
if calls["n"] <= 2:
- raise _rpc_error("namespace rate limit exceeded", RPCStatusCode.RESOURCE_EXHAUSTED)
+ raise _rpc_error(message, status)
return "ok"
- result = await _with_rate_limit_retry(operation, MagicMock())
+ result = await _with_transient_rpc_retry(operation, MagicMock())
assert result == "ok"
assert calls["n"] == 3
# Backoff grows per attempt per `min(2 * attempt, 30)`: 2s after the 1st failure, 4s after the 2nd.
assert sleep.await_args_list == [call(2), call(4)]
+ @pytest.mark.parametrize(
+ "message,status",
+ [
+ ("namespace rate limit exceeded", RPCStatusCode.RESOURCE_EXHAUSTED),
+ ("downstream duration timeout", RPCStatusCode.DEADLINE_EXCEEDED),
+ ],
+ )
@patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.temporalio.temporalio.asyncio.sleep",
new_callable=AsyncMock,
)
- async def test_persistent_rate_limit_is_reraised(self, sleep):
+ async def test_persistent_transient_error_is_reraised(self, sleep, message, status):
async def operation():
- raise _rpc_error("namespace rate limit exceeded", RPCStatusCode.RESOURCE_EXHAUSTED)
+ raise _rpc_error(message, status)
with pytest.raises(RPCError):
- await _with_rate_limit_retry(operation, MagicMock(), max_attempts=4)
+ await _with_transient_rpc_retry(operation, MagicMock(), max_attempts=4)
# Bounded attempts leave Temporal to retry; backs off between attempts but not after the last.
assert sleep.await_args_list == [call(2), call(4), call(6)]
@@ -144,11 +158,11 @@ class TestRateLimitRetry:
"products.warehouse_sources.backend.temporal.data_imports.sources.temporalio.temporalio.asyncio.sleep",
new_callable=AsyncMock,
)
- async def test_non_rate_limit_rpc_error_is_not_retried(self, sleep):
+ async def test_non_transient_rpc_error_is_not_retried(self, sleep):
async def operation():
raise _rpc_error("workflow execution not found for", RPCStatusCode.NOT_FOUND)
with pytest.raises(RPCError):
- await _with_rate_limit_retry(operation, MagicMock())
+ await _with_transient_rpc_retry(operation, MagicMock())
assert sleep.await_count == 0