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(instagram): retry a connection reset mid-response body (#99645)

Tom Owersf2f90e4d69ea
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
index 7b8a7dbf675e..90154f812e48 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
@@ -231,7 +231,17 @@ class InstagramClient:
return False
@retry(
- retry=retry_if_exception_type((InstagramRetryableError, requests.ReadTimeout, requests.ConnectionError)),
+ retry=retry_if_exception_type(
+ (
+ InstagramRetryableError,
+ requests.ReadTimeout,
+ requests.ConnectionError,
+ # Raised instead of ConnectionError when the reset lands mid-body on a
+ # chunked response (see requests.models.Response.generate) — the same
+ # transient network blip, just caught at a different layer of urllib3.
+ requests.exceptions.ChunkedEncodingError,
+ )
+ ),
stop=stop_after_attempt(MAX_RETRY_ATTEMPTS),
wait=wait_exponential_jitter(initial=2, max=120),
reraise=True,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
index 96c6d3d98330..66a395f2cc10 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
@@ -6,6 +6,7 @@ from urllib.parse import parse_qs, urlparse
import pytest
from unittest import mock
+import requests
import structlog
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
@@ -285,6 +286,25 @@ class TestInstagramTransport:
assert len(session.requested_urls) == MAX_RETRY_ATTEMPTS
+ def test_a_connection_reset_mid_response_body_is_retried(self) -> None:
+ # A reset that lands while urllib3 is still reading a chunked body surfaces as
+ # ChunkedEncodingError rather than ConnectionError, but it's the same transient
+ # network blip and must be retried the same way.
+ session = FakeSession()
+ session.get = mock.Mock( # type: ignore[method-assign]
+ side_effect=[
+ requests.exceptions.ChunkedEncodingError("Connection broken: ConnectionResetError(104, ...)"),
+ FakeResponse(200, {"id": ACCOUNT_ID}),
+ ]
+ )
+ with mock.patch(f"{MODULE}.make_tracked_session", return_value=session):
+ client = InstagramClient("tok", "v23.0", LOGGER)
+
+ body = client.get(client.build_url(ACCOUNT_ID))
+
+ assert body == {"id": ACCOUNT_ID}
+ assert session.get.call_count == 2
+
@pytest.mark.parametrize(
"status_code,code,prefix",
[