: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(intercom): retry idempotent search/list POSTs on transient timeouts (#62133)
Tom Owers5051dc93fd43
diff --git a/posthog/temporal/data_imports/sources/intercom/intercom.py b/posthog/temporal/data_imports/sources/intercom/intercom.pyindex eed4fa595b9b..107e08e4768a 100644--- a/posthog/temporal/data_imports/sources/intercom/intercom.py+++ b/posthog/temporal/data_imports/sources/intercom/intercom.py@@ -2,9 +2,10 @@ from collections.abc import AsyncIterable, Callable, Iterable, Iteratorfrom typing import Any, Optionalfrom requests import Request, Response, Session+from urllib3.util.retry import Retryfrom posthog.temporal.data_imports.pipelines.pipeline.typings import SourceResponse-from posthog.temporal.data_imports.sources.common.http import make_tracked_session+from posthog.temporal.data_imports.sources.common.http import DEFAULT_RETRY, make_tracked_sessionfrom posthog.temporal.data_imports.sources.common.rest_source import RESTAPIConfig, rest_api_resourcefrom posthog.temporal.data_imports.sources.common.rest_source.paginators import (BasePaginator,@@ -31,6 +32,23 @@ def _auth_headers(access_token: str) -> dict[str, str]:return {"Authorization": f"Bearer {access_token}", **_default_headers()}+# The substream walk reaches `/conversations/search` and `/companies/list` via+# POST. The shared `DEFAULT_RETRY` excludes POST from `allowed_methods`, so a+# transient read timeout on those calls is *not* retried — unlike the GET calls+# in the same walk, which retry transparently. These POSTs are read-only,+# idempotent queries (the body just carries the query + cursor), so it's safe to+# retry them on transient read timeouts and 429/5xx like everything else.+# Derived from DEFAULT_RETRY so the shared policy stays the single source of+# truth — the only intentional difference is adding POST to allowed_methods.+_INTERCOM_RETRY = Retry(+ total=DEFAULT_RETRY.total,+ backoff_factor=DEFAULT_RETRY.backoff_factor,+ status_forcelist=DEFAULT_RETRY.status_forcelist,+ allowed_methods=frozenset(DEFAULT_RETRY.allowed_methods or ()) | {"POST"},+ raise_on_status=DEFAULT_RETRY.raise_on_status,+)++def _make_intercom_session(access_token: str) -> Session:"""Build a tracked session with Intercom auth + default headers baked in.@@ -38,7 +56,7 @@ def _make_intercom_session(access_token: str) -> Session:keep the underlying TCP+TLS connection alive — the substream generators(one GET per parent row) are the main beneficiary."""- return make_tracked_session(headers=_auth_headers(access_token))+ return make_tracked_session(headers=_auth_headers(access_token), retry=_INTERCOM_RETRY)class IntercomSearchPaginator(BasePaginator):diff --git a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.pyindex 82b45b279a89..939b079c1832 100644--- a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py+++ b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py@@ -5,6 +5,7 @@ import pytestfrom unittest import mockfrom requests import Request, Response+from requests.adapters import HTTPAdapterfrom posthog.temporal.data_imports.sources.common.rest_source.paginators import (JSONResponseCursorPaginator,@@ -20,6 +21,7 @@ from posthog.temporal.data_imports.sources.intercom.intercom import (_company_segments_generator,_conversation_parts_generator,_iter_companies,+ _make_intercom_session,get_resource,intercom_source,validate_credentials,@@ -299,6 +301,22 @@ class TestSubstreamGenerators:assert mock_session.get.call_args_list[0].args[0] == next_url+class TestSubstreamSessionRetries:+ def test_idempotent_search_posts_are_retryable(self):+ # The substream walk reaches `/conversations/search` and `/companies/list`+ # via POST. The shared default retry policy excludes POST, so a transient+ # read timeout on those calls would propagate unretried (unlike the GETs in+ # the same walk). These POSTs are read-only/idempotent, so the session must+ # retry them on transient read timeouts and 429/5xx.+ session = _make_intercom_session("token")+ retry = cast(HTTPAdapter, session.get_adapter(INTERCOM_API_BASE)).max_retries+ allowed_methods = cast("frozenset[str]", retry.allowed_methods)++ assert {"GET", "POST"} <= set(allowed_methods)+ assert retry.total == 3+ assert 429 in (retry.status_forcelist or ())++class TestIntercomSource:@pytest.mark.parametrize("endpoint", list(INTERCOM_ENDPOINTS.keys()))def test_source_response_metadata(self, endpoint: str):