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(stripe): retry connection reset mid-stream in-process (#65713)

Tom Owersdb4ed9d4ec46
diff --git a/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py b/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py
index d382c76592fe..501c776eea79 100644
--- a/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py
+++ b/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py
@@ -6,6 +6,7 @@ import pytest
from unittest import mock
import stripe as stripe_lib
+import requests
from parameterized import parameterized
from stripe._http_client import HTTPClient
@@ -707,6 +708,41 @@ def test_stripe_http_client_retries_rate_limits(_name, status_code, num_retries,
)
+ [
+ # (cause, num_retries, max_network_retries, expected)
+ # A connection reset mid-response body surfaces as a ChunkedEncodingError, which Stripe
+ # wraps in a non-retryable APIConnectionError — we retry it while budget remains.
+ ("connection_reset_retried", requests.exceptions.ChunkedEncodingError("Connection broken"), 0, 2, True),
+ # ...but stops once the retry budget is exhausted, so we don't loop forever.
+ (
+ "connection_reset_budget_exhausted",
+ requests.exceptions.ChunkedEncodingError("Connection broken"),
+ 2,
+ 2,
+ False,
+ ),
+ # An SSL error is deliberately non-retryable in the SDK — we must not start retrying it.
+ ("ssl_error_not_retried", requests.exceptions.SSLError("bad cert"), 0, 2, False),
+ # An APIConnectionError with no wrapped cause has nothing to identify as a reset.
+ ("no_cause_not_retried", None, 0, 2, False),
+ ]
+)
+def test_stripe_http_client_retries_connection_reset(_name, cause, num_retries, max_network_retries, expected):
+ """A connection reset while paging surfaces from requests as a ChunkedEncodingError, which the
+ SDK declines to retry (only Timeout/ConnectionError are retryable) so it crashes the import.
+ Our client retries that transient reset in-process while leaving SSL errors non-retryable."""
+ client = _tracked_stripe_http_client()
+ # response=None is how the SDK signals a connection error rather than an HTTP response.
+ error = stripe_lib.APIConnectionError("Unexpected error communicating with Stripe.")
+ if cause is not None:
+ error.__cause__ = cause
+
+ assert (
+ client._should_retry(None, error, num_retries=num_retries, max_network_retries=max_network_retries) is expected
+ )
+
+
def test_validate_credentials_nested_resources_have_registered_parents():
"""Invariant: every StripeNestedResource's `parent_name` must point at a key that is
also registered as a top-level StripeResource in _build_resources. validate_credentials
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
index 0ff693e58780..c1022a1a87af 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
@@ -7,6 +7,7 @@ from typing import Any, Literal, Optional, Union, cast, get_args, get_type_hints
import orjson
import stripe as stripe_lib
import pyarrow as pa
+import requests
from asgiref.sync import async_to_sync
from stripe import ListObject, RequestsClient, StripeClient
from stripe._base_address import BaseAddresses
@@ -57,6 +58,18 @@ LOGGER = get_logger(__name__)
DEFAULT_LIMIT = 100
+def _is_retryable_connection_reset(error: stripe_lib.APIConnectionError) -> bool:
+ """A connection reset *mid-response* surfaces from ``requests`` as a ``ChunkedEncodingError``
+ (the body stream broke after the headers arrived). Stripe's ``_handle_request_error`` only
+ flags ``Timeout`` and ``ConnectionError`` as retryable, so it wraps this in an
+ ``APIConnectionError`` with ``should_retry=False`` and the SDK gives up — the error then
+ propagates straight out of ``auto_paging_iter`` and fails the whole import. The reset is
+ transient and our reads are idempotent list/GET calls, so it is safe to retry within the SDK's
+ bounded backoff. Matching ``ChunkedEncodingError`` (not ``SSLError`` or other
+ ``RequestException``\\ s the SDK also declines) keeps this scoped to the mid-stream drop."""
+ return isinstance(error.__cause__, requests.exceptions.ChunkedEncodingError)
+
+
class _RateLimitRetryingRequestsClient(RequestsClient):
"""Stripe's SDK retries 409/5xx (and whatever ``Stripe-Should-Retry`` advises) but never
retries 429s on its own — ``_should_retry`` excludes them. A rate limit during a large sync,
@@ -65,7 +78,9 @@ class _RateLimitRetryingRequestsClient(RequestsClient):
Opt 429 into the SDK's existing ``Retry-After``-aware exponential backoff so transient rate
limits are absorbed in-process (bounded by ``max_network_retries``) instead of crashing the
- run. Our Stripe reads are list/GET calls, so retrying them is idempotent."""
+ run. We also retry a connection reset that drops the response mid-body — the SDK declines it
+ (see ``_is_retryable_connection_reset``). Our Stripe reads are list/GET calls, so retrying them
+ is idempotent."""
def _should_retry(
self,
@@ -76,11 +91,14 @@ class _RateLimitRetryingRequestsClient(RequestsClient):
) -> bool:
if super()._should_retry(response, api_connection_error, num_retries, max_network_retries):
return True
- # The base logic already enforced the retry budget and declined; the only retryable case
- # it leaves on the table is a 429, which the SDK omits but which is safe to retry here.
+ # The base logic already enforced the retry budget and declined; the only retryable cases
+ # it leaves on the table are a 429 (the SDK omits it) and a connection reset mid-response
+ # body, both safe to retry on our idempotent list/GET calls.
if num_retries >= (max_network_retries or 0):
return False
- return response is not None and response[1] == 429
+ if response is not None:
+ return response[1] == 429
+ return api_connection_error is not None and _is_retryable_connection_reset(api_connection_error)
def _tracked_stripe_http_client() -> RequestsClient: