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(warehouse-sources): retry a client-side temporal rpc timeout (#101080)

Tom Owers90aeea8987ff
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
index b35d94bea9fa..2b33c0c55d15 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
@@ -532,13 +532,27 @@ def _mark_job_completed(export_signal: ExportSignalMessage) -> None:
_release_pipeline_lock_for_job(export_signal)
+# tonic's timeout layer cancels a call that outruns the client's per-request RPC deadline and
+# surfaces it as status CANCELLED with the message "Timeout expired" — the client-side analog of
+# DEADLINE_EXCEEDED above — and a connection closed mid-request as CANCELLED with "operation was
+# canceled". Match the phrase rather than the whole status so a genuine cancellation still
+# surfaces. Same phrases the data-imports source client (sources/temporalio/temporalio.py) and the
+# Temporal schedule helpers (posthog/temporal/common/schedule.py) treat as transient.
+_RETRYABLE_RPC_MESSAGES_BY_STATUS: dict[RPCStatusCode, tuple[str, ...]] = {
+ RPCStatusCode.CANCELLED: ("Timeout expired", "operation was canceled"),
+}
+
+
def _is_retryable_temporal_rpc_error(exc: BaseException) -> bool:
# These fire-and-forget starts run outside a Temporal workflow, so unlike
# `workflow.start_child_workflow` they get none of the server-side retry a durable
# workflow command would have — a bare client RPC timeout would otherwise drop the
# trigger permanently.
- if isinstance(exc, RPCError) and exc.status in (RPCStatusCode.DEADLINE_EXCEEDED, RPCStatusCode.UNAVAILABLE):
- return True
+ if isinstance(exc, RPCError):
+ if exc.status in (RPCStatusCode.DEADLINE_EXCEEDED, RPCStatusCode.UNAVAILABLE):
+ return True
+ if any(phrase in exc.message for phrase in _RETRYABLE_RPC_MESSAGES_BY_STATUS.get(exc.status, ())):
+ return True
# `async_connect()` runs before any service client exists, so a transient failure to
# reach the Temporal frontend (DNS blip, connection refused/reset) surfaces as the Rust
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py
index f3c54625637c..d331b65118ad 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py
@@ -601,9 +601,13 @@ class TestPostImportTrigger:
[
# Any start failure (e.g. no Temporal env vars on the load deployment) must
# not fail the load; it is logged and captured.
- ("start_failure_is_captured", RuntimeError("no temporal"), True),
+ ("start_failure_is_captured", RuntimeError("no temporal"), True, 1),
# An id collision means a register is already in flight for this schema.
- ("already_started_is_benign", WorkflowAlreadyStartedError("wf-id", "wf-type"), False),
+ ("already_started_is_benign", WorkflowAlreadyStartedError("wf-id", "wf-type"), False, 1),
+ # A genuine cancellation must not be mistaken for the client-side timeout that also
+ # reports CANCELLED — only the "Timeout expired" / "operation was canceled" phrases
+ # are transient, so this one is captured on the first attempt, not retried.
+ ("genuine_cancel_is_captured", RPCError("Cancelled by caller", RPCStatusCode.CANCELLED, b""), True, 1),
]
)
@patch(f"{_PROCESSOR}.capture_exception")
@@ -613,6 +617,7 @@ class TestPostImportTrigger:
_case: str,
error: Exception,
expect_captured: bool,
+ expected_attempts: int,
mock_connect: AsyncMock,
mock_capture: MagicMock,
) -> None:
@@ -630,6 +635,7 @@ class TestPostImportTrigger:
_trigger_post_import_workflow(signal)
+ assert client.start_workflow.call_count == expected_attempts
assert mock_capture.called is expect_captured
def _signal(self) -> MagicMock:
@@ -642,10 +648,22 @@ class TestPostImportTrigger:
signal.source_id = "source-1"
return signal
+ @parameterized.expand(
+ [
+ ("deadline_exceeded", RPCError("Timeout expired", RPCStatusCode.DEADLINE_EXCEEDED, b"")),
+ # tonic cancels a call that outruns the client's own RPC deadline and reports it as
+ # CANCELLED with this message, not DEADLINE_EXCEEDED — the status a bare-frontend
+ # timeout produces. Must be ridden out the same way, not dropped on the first blip.
+ ("client_side_cancel", RPCError("Timeout expired", RPCStatusCode.CANCELLED, b"")),
+ ("lost_connection", RPCError("operation was canceled", RPCStatusCode.CANCELLED, b"")),
+ ]
+ )
@patch(f"{_PROCESSOR}.capture_exception")
@patch("posthog.temporal.common.client.async_connect", new_callable=AsyncMock)
def test_transient_rpc_timeout_is_retried_and_recovers(
self,
+ _case: str,
+ error: RPCError,
mock_connect: AsyncMock,
mock_capture: MagicMock,
) -> None:
@@ -653,9 +671,7 @@ class TestPostImportTrigger:
# none of the server-side retry a `workflow.start_child_workflow` command would
# have — a single transient timeout must not drop the trigger permanently.
client = MagicMock()
- client.start_workflow = AsyncMock(
- side_effect=[RPCError("Timeout expired", RPCStatusCode.DEADLINE_EXCEEDED, b""), None]
- )
+ client.start_workflow = AsyncMock(side_effect=[error, None])
mock_connect.return_value = client
_trigger_post_import_workflow(self._signal())