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(notion): retry connection-break ChunkedEncodingError (#63052)

Tom Owersfd1abb38c73c
diff --git a/posthog/temporal/data_imports/sources/notion/notion.py b/posthog/temporal/data_imports/sources/notion/notion.py
index 22d717e04850..ff32a3cfa0bb 100644
--- a/posthog/temporal/data_imports/sources/notion/notion.py
+++ b/posthog/temporal/data_imports/sources/notion/notion.py
@@ -81,7 +81,18 @@ def _wait_strategy(retry_state: RetryCallState) -> float:
@retry(
- retry=retry_if_exception_type((NotionRetryableError, requests.ReadTimeout, requests.ConnectionError)),
+ # ChunkedEncodingError is a sibling of ConnectionError (both RequestException, not subclasses of
+ # each other): Notion can break the connection mid-response, surfacing as a malformed chunk
+ # ("Connection broken: InvalidChunkLength"). It is a transient connection failure like the others,
+ # so retry it rather than letting it crash the sync.
+ retry=retry_if_exception_type(
+ (
+ NotionRetryableError,
+ requests.ReadTimeout,
+ requests.ConnectionError,
+ requests.exceptions.ChunkedEncodingError,
+ )
+ ),
stop=stop_after_attempt(5),
wait=_wait_strategy,
reraise=True,
diff --git a/posthog/temporal/data_imports/sources/notion/tests/test_notion.py b/posthog/temporal/data_imports/sources/notion/tests/test_notion.py
index 713bf5a8ba56..d75882cb875c 100644
--- a/posthog/temporal/data_imports/sources/notion/tests/test_notion.py
+++ b/posthog/temporal/data_imports/sources/notion/tests/test_notion.py
@@ -185,6 +185,27 @@ class TestNotion:
)
assert exc_info.value.retry_after is None
+ def test_request_retries_chunked_encoding_error(self) -> None:
+ # Notion can break the connection mid-response, which requests surfaces as a
+ # ChunkedEncodingError ("Connection broken: InvalidChunkLength"). It is transient and must be
+ # retried like other connection failures, not propagated as a fatal sync error.
+ attempts = {"count": 0}
+
+ def request(*_args: Any, **_kwargs: Any) -> FakeResponse:
+ attempts["count"] += 1
+ if attempts["count"] == 1:
+ raise requests.exceptions.ChunkedEncodingError("Connection broken: InvalidChunkLength(got length b'')")
+ return FakeResponse({"results": []})
+
+ session = mock.MagicMock()
+ session.request.side_effect = request
+
+ with mock.patch(f"{MODULE}._wait_strategy", return_value=0):
+ result = _request(cast(requests.Session, session), "GET", "/v1/comments", mock.MagicMock(), params={})
+
+ assert result == {"results": []}
+ assert attempts["count"] == 2
+
@parameterized.expand([("5", 5.0), (None, None), ("not-a-number", None)])
def test_parse_retry_after(self, value: str | None, expected: float | None) -> None:
assert _parse_retry_after(value) == expected