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 transient SSL/connection errors on token exchange (#65779)

Tom Owers6cea2939fd51
diff --git a/posthog/temporal/data_imports/sources/shopify/shopify.py b/posthog/temporal/data_imports/sources/shopify/shopify.py
index 9c7bd8840370..c19da82bb6b4 100644
--- a/posthog/temporal/data_imports/sources/shopify/shopify.py
+++ b/posthog/temporal/data_imports/sources/shopify/shopify.py
@@ -280,6 +280,18 @@ def normalize_store_id(raw: str) -> str:
return store_id
+@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)),
+ stop=stop_after_attempt(5),
+ wait=_shopify_backoff,
+ reraise=True,
+)
def _get_shopify_access_token(shopify_store_id: str, shopify_client_id: str, shopify_client_secret: str) -> str:
# Callers pass an already-normalized store id (see normalize_store_id).
access_token_url = SHOPIFY_ACCESS_TOKEN_URL.format(shopify_store_id)
diff --git a/posthog/temporal/data_imports/sources/shopify/tests/test_access_token.py b/posthog/temporal/data_imports/sources/shopify/tests/test_access_token.py
index 493dca6d64cd..2498758e4e49 100644
--- a/posthog/temporal/data_imports/sources/shopify/tests/test_access_token.py
+++ b/posthog/temporal/data_imports/sources/shopify/tests/test_access_token.py
@@ -1,6 +1,8 @@
import pytest
from unittest import mock
+from requests.exceptions import SSLError
+
from posthog.temporal.data_imports.sources.shopify.shopify import (
SHOPIFY_ACCESS_TOKEN_AUTH_ERROR,
_get_shopify_access_token,
@@ -55,6 +57,38 @@ def test_get_access_token_success_returns_token():
assert _get_shopify_access_token("store", "client-id", "client-secret") == "tok"
[email protected]("tenacity.nap.time.sleep")
+def test_get_access_token_retries_ssl_error_then_succeeds(_mock_sleep):
+ # A transient TLS drop on the token endpoint surfaces from `post` as SSLError (a
+ # ConnectionError); it's transient, so reissue the request rather than failing the import.
+ post = mock.MagicMock(
+ side_effect=[
+ SSLError("[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol"),
+ _mock_response(200, ok=True, json_data={"access_token": "tok"}),
+ ]
+ )
+ with mock.patch(
+ "posthog.temporal.data_imports.sources.shopify.shopify.make_tracked_session",
+ return_value=mock.MagicMock(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_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(
+ "posthog.temporal.data_imports.sources.shopify.shopify.make_tracked_session",
+ return_value=mock.MagicMock(post=post),
+ ):
+ with pytest.raises(SSLError):
+ _get_shopify_access_token("store", "client-id", "client-secret")
+ assert post.call_count == 5
+
+
@pytest.mark.parametrize(
"error_message",
[