: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(shopify): retry connection broken mid-stream in-process (#65772)
Tom Owersfa5df224bf81
diff --git a/posthog/temporal/data_imports/sources/shopify/shopify.py b/posthog/temporal/data_imports/sources/shopify/shopify.pyindex 51c805532b31..d71a60e39679 100644--- a/posthog/temporal/data_imports/sources/shopify/shopify.py+++ b/posthog/temporal/data_imports/sources/shopify/shopify.py@@ -6,6 +6,7 @@ from collections.abc import Iteratorfrom typing import Anyimport requests+from requests.exceptions import ChunkedEncodingErrorfrom structlog.types import FilteringBoundLoggerfrom tenacity import RetryCallState, retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter@@ -179,7 +180,12 @@ def _make_paginated_shopify_request(reraise=True,)def execute(vars: dict[str, Any]):- response = sess.post(url, json={"query": graphql_object.query, "variables": vars})+ # `post` reads the body eagerly (stream=False), so a connection dropped mid-stream+ # surfaces here as ChunkedEncodingError. It's transient — retry it like a 5xx below.+ try:+ response = sess.post(url, json={"query": graphql_object.query, "variables": vars})+ except ChunkedEncodingError as e:+ raise ShopifyRetryableError(f"Shopify: connection broken while reading response: {e}") from eif response.status_code >= 500:raise ShopifyRetryableError(f"Shopify: internal error from request {response.status_code} {response.reason}"diff --git a/posthog/temporal/data_imports/sources/shopify/tests/test_rate_limit_retry.py b/posthog/temporal/data_imports/sources/shopify/tests/test_rate_limit_retry.pyindex 18f5803016c6..0861f29e3c03 100644--- a/posthog/temporal/data_imports/sources/shopify/tests/test_rate_limit_retry.py+++ b/posthog/temporal/data_imports/sources/shopify/tests/test_rate_limit_retry.py@@ -1,11 +1,15 @@import pytest+from unittest.mock import MagicMock, patch+from requests.exceptions import ChunkedEncodingErrorfrom tenacity import Future, RetryCallState, Retrying+from posthog.temporal.data_imports.sources.shopify.constants import ABANDONED_CHECKOUTS, SHOPIFY_GRAPHQL_OBJECTSfrom posthog.temporal.data_imports.sources.shopify.shopify import (_SHOPIFY_MAX_THROTTLE_WAIT_SECONDS,ShopifyRetryableError,_get_retryable_error,+ _make_paginated_shopify_request,_shopify_retry_wait,_throttle_retry_after,)@@ -81,3 +85,47 @@ def test_retry_wait_honors_throttle_refill_time():def test_retry_wait_falls_back_to_backoff_without_retry_after():wait = _shopify_retry_wait(_retry_state(ShopifyRetryableError("internal error")))assert 1.0 <= wait <= 2.0 # initial=1, max=30, jitter up to 1s on attempt 1+++def _ok_page() -> MagicMock:+ response = MagicMock()+ response.status_code = 200+ response.json.return_value = {+ "data": {ABANDONED_CHECKOUTS: {"nodes": [{"id": "1"}], "pageInfo": {"hasNextPage": False, "endCursor": None}}}+ }+ return response+++@patch("tenacity.nap.time.sleep")+def test_request_retries_connection_broken_mid_stream_then_succeeds(_mock_sleep):+ # A connection dropped mid-response surfaces from `post` as ChunkedEncodingError; it's+ # transient, so the request is reissued rather than failing the import.+ sess = MagicMock()+ sess.post.side_effect = [+ ChunkedEncodingError("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)"),+ _ok_page(),+ ]++ batches = list(+ _make_paginated_shopify_request(+ "https://example.invalid/graphql", sess, SHOPIFY_GRAPHQL_OBJECTS[ABANDONED_CHECKOUTS], MagicMock()+ )+ )++ assert batches == [[{"id": "1"}]]+ assert sess.post.call_count == 2+++@patch("tenacity.nap.time.sleep")+def test_request_reraises_retryable_after_persistent_connection_broken(_mock_sleep):+ sess = MagicMock()+ sess.post.side_effect = ChunkedEncodingError("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)")++ with pytest.raises(ShopifyRetryableError):+ list(+ _make_paginated_shopify_request(+ "https://example.invalid/graphql", sess, SHOPIFY_GRAPHQL_OBJECTS[ABANDONED_CHECKOUTS], MagicMock()+ )+ )++ assert sess.post.call_count == 5