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(shopify): retry connection broken mid-stream on token exchange (#65775)

Tom Owers1ac0a32c8c6c
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
index d14208df6c7e..16263c01c410 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
@@ -287,11 +287,15 @@ def normalize_store_id(raw: str) -> str:
@retry(
# A transient TLS/connection drop on the token endpoint (e.g. SSL EOF, proxy/egress hiccup,
# connect/read timeout) surfaces from `post` as requests ConnectionError/Timeout — SSLError
- # is a ConnectionError. The adapter's own urllib3 retries back off for only ~1.5s, too short
- # to ride out a multi-second blip. Minting a token is idempotent, so reissue with backoff
- # rather than failing the whole import. 4xx/5xx are raised as plain Exceptions below and so
- # are untouched here — auth failures still fail fast.
- retry=retry_if_exception_type((requests.exceptions.ConnectionError, requests.exceptions.Timeout)),
+ # is a ConnectionError. A connection dropped mid-response surfaces as ChunkedEncodingError,
+ # which is a RequestException rather than a ConnectionError, so it must be listed explicitly.
+ # The adapter's own urllib3 retries back off for only ~1.5s, too short to ride out a
+ # multi-second blip. Minting a token is idempotent, so reissue with backoff rather than failing
+ # the whole import. 4xx/5xx are raised as plain Exceptions below and so are untouched here —
+ # auth failures still fail fast.
+ retry=retry_if_exception_type(
+ (requests.exceptions.ConnectionError, requests.exceptions.Timeout, requests.exceptions.ChunkedEncodingError)
+ ),
stop=stop_after_attempt(5),
wait=_shopify_backoff,
reraise=True,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py
index dfcf6ec3ce21..06308cd2b39e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py
@@ -1,7 +1,7 @@
import pytest
from unittest import mock
-from requests.exceptions import SSLError
+from requests.exceptions import ChunkedEncodingError, SSLError
from products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify import (
SHOPIFY_ACCESS_TOKEN_AUTH_ERROR,
@@ -18,13 +18,17 @@ def _mock_response(status_code: int, ok: bool, json_data: dict | None = None) ->
return response
-def _patched_token_call(response: mock.MagicMock):
+def _patched_token_post(post: mock.MagicMock):
return mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify.make_tracked_session",
- return_value=mock.MagicMock(post=mock.MagicMock(return_value=response)),
+ return_value=mock.MagicMock(post=post),
)
+def _patched_token_call(response: mock.MagicMock):
+ return _patched_token_post(mock.MagicMock(return_value=response))
+
+
@pytest.mark.parametrize("status_code", [400, 401, 403, 404])
def test_get_access_token_4xx_is_non_retryable(status_code):
with _patched_token_call(_mock_response(status_code, ok=False)):
@@ -67,10 +71,7 @@ def test_get_access_token_retries_ssl_error_then_succeeds(_mock_sleep):
_mock_response(200, ok=True, json_data={"access_token": "tok"}),
]
)
- with mock.patch(
- "products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify.make_tracked_session",
- return_value=mock.MagicMock(post=post),
- ):
+ with _patched_token_post(post):
assert _get_shopify_access_token("store", "client-id", "client-secret") == "tok"
assert post.call_count == 2
@@ -80,15 +81,38 @@ def test_get_access_token_reraises_after_persistent_ssl_error(_mock_sleep):
# A persistent connection failure must exhaust the retry budget and re-raise the original
# error rather than being swallowed, so the import still fails (and Temporal retries it).
post = mock.MagicMock(side_effect=SSLError("[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred"))
- with mock.patch(
- "products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify.make_tracked_session",
- return_value=mock.MagicMock(post=post),
- ):
+ with _patched_token_post(post):
with pytest.raises(SSLError):
_get_shopify_access_token("store", "client-id", "client-secret")
assert post.call_count == 5
[email protected]("tenacity.nap.time.sleep")
+def test_get_access_token_retries_connection_broken_then_succeeds(_mock_sleep):
+ # A connection dropped mid-response surfaces from `post` as ChunkedEncodingError; it's
+ # transient, so reissue the request rather than letting it fail the import.
+ post = mock.MagicMock(
+ side_effect=[
+ ChunkedEncodingError("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)"),
+ _mock_response(200, ok=True, json_data={"access_token": "tok"}),
+ ]
+ )
+ with _patched_token_post(post):
+ assert _get_shopify_access_token("store", "client-id", "client-secret") == "tok"
+ assert post.call_count == 2
+
+
[email protected]("tenacity.nap.time.sleep")
+def test_get_access_token_reraises_after_exhausting_retries(_mock_sleep):
+ # When the connection breaks on every attempt, the retry must stop after the attempt cap
+ # and reraise the underlying error rather than swallowing it.
+ post = mock.MagicMock(side_effect=ChunkedEncodingError("Connection broken: IncompleteRead(0 bytes read)"))
+ with _patched_token_post(post):
+ with pytest.raises(ChunkedEncodingError):
+ _get_shopify_access_token("store", "client-id", "client-secret")
+ assert post.call_count == 5
+
+
@pytest.mark.parametrize(
"error_message",
[