: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(linear): retry HTTP 429 rate limits instead of failing the sync (#63027)
Tom Owersaa8b796ddf1d
diff --git a/posthog/temporal/data_imports/sources/linear/linear.py b/posthog/temporal/data_imports/sources/linear/linear.pyindex 7752f7b24e68..948720a716f1 100644--- a/posthog/temporal/data_imports/sources/linear/linear.py+++ b/posthog/temporal/data_imports/sources/linear/linear.py@@ -60,6 +60,13 @@ def _make_paginated_request(if response.status_code >= 500:raise LinearRetryableError(f"Linear: server error {response.status_code}")+ # Linear answers HTTP-level rate limits with a 429 and an HTML body (not GraphQL JSON),+ # so this must be caught before the JSON parse below. Otherwise response.json() raises a+ # JSONDecodeError that escalates to a plain, non-retryable Exception instead of being+ # retried with backoff like the GraphQL-level RATELIMITED case.+ if response.status_code == 429:+ raise LinearRetryableError("Linear: rate limited (429)")+try:payload = response.json()except Exception:diff --git a/posthog/temporal/data_imports/sources/linear/test_linear.py b/posthog/temporal/data_imports/sources/linear/test_linear.pyindex 55860a687033..e533aee5112f 100644--- a/posthog/temporal/data_imports/sources/linear/test_linear.py+++ b/posthog/temporal/data_imports/sources/linear/test_linear.py@@ -9,6 +9,7 @@ from parameterized import parameterizedfrom posthog.temporal.data_imports.sources.linear.linear import (LinearResumeConfig,+ LinearRetryableError,_make_paginated_request,linear_source,)@@ -29,6 +30,17 @@ def _make_response(nodes: list[dict[str, Any]], has_next_page: bool, end_cursor:return response+def _make_rate_limited_response() -> MagicMock:+ """Mimic Linear's HTTP-level 429: an HTML body that fails JSON parsing."""+ response = MagicMock()+ response.status_code = 429+ response.ok = False+ response.reason = "Too Many Requests"+ response.text = "<!DOCTYPE html><html><head><title>Rate limited</title></head></html>"+ response.json.side_effect = ValueError("Expecting value: line 1 column 1 (char 0)")+ return response++def _capture_post_calls(session: MagicMock, responses: list[MagicMock]) -> list[dict[str, Any]]:"""Configure session.post to record a deep-copied snapshot of variables at each call.@@ -150,6 +162,57 @@ class TestMakePaginatedRequest:manager.save_state.assert_not_called()assert session.post.call_count == 1+ @patch("time.sleep", return_value=None)+ @patch("posthog.temporal.data_imports.sources.linear.linear.make_tracked_session")+ def test_http_429_is_retried_then_succeeds(self, mock_session_cls: MagicMock, _mock_sleep: MagicMock) -> None:+ # Linear returns an HTML 429 page that fails JSON parsing. It must be retried with backoff,+ # not surfaced as a non-retryable JSONDecodeError/Exception.+ session = MagicMock()+ session.post.side_effect = [+ _make_rate_limited_response(),+ _make_response([{"id": "a"}], False, None),+ ]+ mock_session_cls.return_value = session++ manager = _make_resumable_manager()+ logger = MagicMock()++ pages = list(+ _make_paginated_request(+ access_token="tok",+ endpoint_name="issues",+ logger=logger,+ resumable_source_manager=manager,+ )+ )++ assert pages == [[{"id": "a"}]]+ assert session.post.call_count == 2++ @patch("time.sleep", return_value=None)+ @patch("posthog.temporal.data_imports.sources.linear.linear.make_tracked_session")+ def test_persistent_http_429_raises_retryable_error(+ self, mock_session_cls: MagicMock, _mock_sleep: MagicMock+ ) -> None:+ session = MagicMock()+ session.post.side_effect = [_make_rate_limited_response() for _ in range(5)]+ mock_session_cls.return_value = session++ manager = _make_resumable_manager()+ logger = MagicMock()++ with pytest.raises(LinearRetryableError):+ list(+ _make_paginated_request(+ access_token="tok",+ endpoint_name="issues",+ logger=logger,+ resumable_source_manager=manager,+ )+ )++ assert session.post.call_count == 5+class TestLinearSource:def test_source_response_wires_primary_key_and_items(self) -> None: