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(hubspot): retry 408 request timeouts instead of failing the sync (#114936)

Tom Owersf4884deafebb
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/helpers.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/helpers.py
index 550054d33a76..1badf1a7aed4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/helpers.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/helpers.py
@@ -21,11 +21,12 @@ BASE_URL = "https://api.hubapi.com/"
def _is_retryable_status(status_code: int) -> bool:
"""Transient HubSpot statuses that warrant a backoff-and-retry rather than failing the sync.
- 429 and 5xx are the usual transient signals. Unrecognised 4xx codes (e.g. the non-standard 477
- HubSpot's edge has been observed returning during brief incidents) aren't actionable client
- errors, so treat any unknown 4xx as transient too instead of crashing the import on it.
+ 408, 429 and 5xx are the usual transient signals. Unrecognised 4xx codes (e.g. the
+ non-standard 477 HubSpot's edge has been observed returning during brief incidents) aren't
+ actionable client errors, so treat any unknown 4xx as transient too instead of crashing the
+ import on it.
"""
- if status_code == 429 or status_code >= 500:
+ if status_code in (408, 429) or status_code >= 500:
return True
if 400 <= status_code < 500:
try:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/hubspot.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/hubspot.py
index 7b9a7d6cbb95..99f342d2d8ae 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/hubspot.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/hubspot.py
@@ -365,7 +365,9 @@ def get_rows(
headers = _get_headers(api_key)
raise HubspotRetryableError(f"Hubspot API 401 - refreshed token, retrying: url={page_url}")
- if response.status_code == 429 or response.status_code >= 500:
+ # 408 is a transient request timeout on HubSpot's side; retry it like 429/5xx rather
+ # than failing the whole sync.
+ if response.status_code in (408, 429) or response.status_code >= 500:
raise HubspotRetryableError(f"Hubspot API error (retryable): status={response.status_code}, url={page_url}")
if not response.ok:
@@ -470,7 +472,9 @@ def _batch_read_associations(
headers.update(_get_headers(new_api_key))
raise HubspotRetryableError(f"Hubspot v4 associations 401 - refreshed token, retrying: url={url}")
- if response.status_code == 429 or response.status_code >= 500:
+ # 408 is a transient request timeout on HubSpot's side; retry it like 429/5xx rather
+ # than failing the whole sync.
+ if response.status_code in (408, 429) or response.status_code >= 500:
raise HubspotRetryableError(
f"Hubspot v4 associations error (retryable): status={response.status_code}, url={url}"
)
@@ -645,7 +649,9 @@ def get_rows_via_search(
headers.update(_get_headers(api_key))
raise HubspotRetryableError(f"Hubspot search 401 - refreshed token, retrying: url={search_url}")
- if response.status_code == 429 or response.status_code >= 500:
+ # 408 is a transient request timeout on HubSpot's side; retry it like 429/5xx rather
+ # than failing the whole sync.
+ if response.status_code in (408, 429) or response.status_code >= 500:
raise HubspotRetryableError(
f"Hubspot search error (retryable): status={response.status_code}, url={search_url}"
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_helpers.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_helpers.py
index 7b816be69c9f..fc1f09dead27 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_helpers.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_helpers.py
@@ -60,6 +60,7 @@ def _no_backoff_sleep() -> Any:
(409, False),
(422, False),
(451, False),
+ (408, True),
(429, True),
(477, True), # non-standard 4xx HubSpot's edge returns during transient incidents
(499, True),
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_search.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_search.py
index 0e90d0532eb9..a36867cd34d1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_search.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_hubspot_search.py
@@ -793,3 +793,41 @@ class TestGetRowsFullRefresh:
assert len(captured_urls) == 2
assert captured_urls[0] == captured_urls[1]
assert sum(t.num_rows for t in tables) == 1
+
+ def test_408_is_retried_then_succeeds(self) -> None:
+ # Regression: a 408 (Request Timeout) used to fall through to raise_for_hubspot_status
+ # and crash the whole sync instead of being retried like a 429/5xx.
+ from products.warehouse_sources.backend.temporal.data_imports.sources.hubspot.hubspot import get_rows
+
+ manager = _make_manager()
+ logger = MagicMock()
+
+ timeout_resp = _make_response(408, text="Request Timeout")
+ good = _make_response(200, {"results": [{"id": "1", "properties": {"hs_object_id": "1"}}]})
+
+ iter_resp = iter([timeout_resp, good])
+ captured_urls: list[str] = []
+
+ def _get(url, headers=None, timeout=None): # noqa: ARG001
+ captured_urls.append(url)
+ return next(iter_resp)
+
+ with patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.hubspot.hubspot.make_tracked_session",
+ new=lambda *_a, **_k: type("_S", (), {"get": staticmethod(_get)})(),
+ ):
+ tables = list(
+ get_rows(
+ api_key="k",
+ refresh_token="r",
+ endpoint="deals",
+ logger=logger,
+ resumable_source_manager=manager,
+ include_custom_props=False,
+ api_version=HUBSPOT_API_VERSION_V3,
+ )
+ )
+
+ assert len(captured_urls) == 2
+ assert captured_urls[0] == captured_urls[1]
+ assert sum(t.num_rows for t in tables) == 1