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 chunked-encoding errors during page fetch (#72511)

Tom Owers3321ffd6ee01
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 8d21745368d7..b9f9895afd8f 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
@@ -332,7 +332,14 @@ def get_rows(
)
@retry(
- retry=retry_if_exception_type((HubspotRetryableError, requests.ReadTimeout, requests.ConnectionError)),
+ retry=retry_if_exception_type(
+ (
+ HubspotRetryableError,
+ requests.ReadTimeout,
+ requests.ConnectionError,
+ requests.exceptions.ChunkedEncodingError,
+ )
+ ),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=True,
@@ -425,7 +432,14 @@ def _batch_read_associations(
by_from: dict[str, list[dict[str, Any]]] = {}
@retry(
- retry=retry_if_exception_type((HubspotRetryableError, requests.ReadTimeout, requests.ConnectionError)),
+ retry=retry_if_exception_type(
+ (
+ HubspotRetryableError,
+ requests.ReadTimeout,
+ requests.ConnectionError,
+ requests.exceptions.ChunkedEncodingError,
+ )
+ ),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=True,
@@ -590,7 +604,14 @@ def get_rows_via_search(
last_cursor_ms = current_lower
@retry(
- retry=retry_if_exception_type((HubspotRetryableError, requests.ReadTimeout, requests.ConnectionError)),
+ retry=retry_if_exception_type(
+ (
+ HubspotRetryableError,
+ requests.ReadTimeout,
+ requests.ConnectionError,
+ requests.exceptions.ChunkedEncodingError,
+ )
+ ),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=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 9e81fbd073b6..6ea8c628bc71 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
@@ -5,7 +5,10 @@ from typing import Any
import pytest
from unittest.mock import MagicMock, patch
-from requests.exceptions import JSONDecodeError as RequestsJSONDecodeError
+from requests.exceptions import (
+ ChunkedEncodingError,
+ JSONDecodeError as RequestsJSONDecodeError,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.hubspot.hubspot import (
HubspotPathologicalWindowError,
@@ -1100,6 +1103,47 @@ class TestGetRowsFullRefresh:
assert captured_urls[0] == captured_urls[1]
assert sum(t.num_rows for t in tables) == 1
+ def test_chunked_encoding_error_is_retried(self) -> None:
+ # Regression: a connection dropped mid-stream (server closes early during chunked
+ # transfer) raises requests' ChunkedEncodingError, which isn't a ConnectionError
+ # subclass. It must be treated as transient and retried, not crash the whole import.
+ from products.warehouse_sources.backend.temporal.data_imports.sources.hubspot.hubspot import get_rows
+
+ manager = _make_manager()
+ logger = MagicMock()
+
+ good = _make_response(200, {"results": [{"id": "1", "properties": {"hs_object_id": "1"}}]})
+ responses = iter([ChunkedEncodingError("Connection broken"), good])
+ captured_urls: list[str] = []
+
+ def _get(url, headers=None, timeout=None): # noqa: ARG001
+ captured_urls.append(url)
+ next_response = next(responses)
+ if isinstance(next_response, Exception):
+ raise next_response
+ return next_response
+
+ 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,
+ )
+ )
+
+ # The dropped connection was retried (same URL) and the second attempt yielded the row.
+ assert len(captured_urls) == 2
+ assert captured_urls[0] == captured_urls[1]
+ assert sum(t.num_rows for t in tables) == 1
+
class TestExpectedPropertiesBackfill:
def test_cursor_column_present_on_row_after_flatten(self) -> None: