: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(data-warehouse): retry Convex reads on mid-body connection breaks (#69965)
Tom Owersa565449ed849
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/convex/convex.py b/products/warehouse_sources/backend/temporal/data_imports/sources/convex/convex.pyindex b87d1473333b..787babbae20a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/convex/convex.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/convex/convex.py@@ -5,11 +5,14 @@ from collections.abc import Generatorfrom typing import Anyfrom urllib.parse import urlparse+from requests import Responsefrom requests.exceptions import (+ ChunkedEncodingError,ConnectionError as RequestsConnectionError,HTTPError,RequestException,)+from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitterfrom products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.typings import SourceResponsefrom products.warehouse_sources.backend.temporal.data_imports.sources.common.http import (@@ -97,11 +100,25 @@ def _headers(deploy_key: str) -> dict[str, str]:}+@retry(+ retry=retry_if_exception_type(ChunkedEncodingError),+ stop=stop_after_attempt(5),+ wait=wait_exponential_jitter(initial=1, max=30),+ reraise=True,+)+def _convex_get(url: str, deploy_key: str, params: dict[str, Any], timeout: int) -> Response:+ # requests reads the body eagerly (stream=False), so a connection broken mid-chunk surfaces+ # here as ChunkedEncodingError — it happens after the response headers, so _CONVEX_RETRY (a+ # urllib3 Retry, which only covers pre-response failures) never sees it. It's transient and+ # every Convex read is an idempotent GET, so a fresh request re-fetches the page.+ return make_tracked_session(retry=_CONVEX_RETRY).get(+ url, headers=_headers(deploy_key), params=params, timeout=timeout+ )++def get_json_schemas(deploy_url: str, deploy_key: str) -> dict[str, Any]:url = f"{deploy_url.rstrip('/')}/api/json_schemas"- response = make_tracked_session(retry=_CONVEX_RETRY).get(- url, headers=_headers(deploy_key), params={"deltaSchema": "true", "format": "json"}, timeout=30- )+ response = _convex_get(url, deploy_key, {"deltaSchema": "true", "format": "json"}, timeout=30)response.raise_for_status()return response.json()@@ -134,9 +151,7 @@ def list_snapshot(if snapshot is not None:params["snapshot"] = snapshot- response = make_tracked_session(retry=_CONVEX_RETRY).get(- base_url, headers=_headers(deploy_key), params=params, timeout=60- )+ response = _convex_get(base_url, deploy_key, params, timeout=60)response.raise_for_status()data = response.json()@@ -196,9 +211,7 @@ def document_deltas(while True:params: dict[str, Any] = {"tableName": table_name, "cursor": current_cursor, "format": "json"}- response = make_tracked_session(retry=_CONVEX_RETRY).get(- base_url, headers=_headers(deploy_key), params=params, timeout=60- )+ response = _convex_get(base_url, deploy_key, params, timeout=60)if response.status_code == 400:error_data = response.json()diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/convex/tests/test_convex.py b/products/warehouse_sources/backend/temporal/data_imports/sources/convex/tests/test_convex.pyindex 899db77d3393..79c0073434d0 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/convex/tests/test_convex.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/convex/tests/test_convex.py@@ -5,7 +5,7 @@ import pytestfrom unittest.mock import MagicMock, Mock, patchfrom parameterized import parameterized-from requests.exceptions import HTTPError+from requests.exceptions import ChunkedEncodingError, HTTPErrorfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom products.warehouse_sources.backend.temporal.data_imports.sources.convex.convex import (@@ -13,6 +13,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.convex.conConvexResumeConfig,InvalidDeployUrlError,InvalidWindowError,+ _convex_get,convex_source,document_deltas,list_snapshot,@@ -256,6 +257,27 @@ class TestDocumentDeltasResumable:manager.save_state.assert_not_called()+class TestConvexChunkedEncodingRetry:+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.convex.convex.make_tracked_session")+ def test_document_deltas_retries_on_chunked_encoding_error(self, mock_get: Mock) -> None:+ # A connection broken mid-body surfaces as ChunkedEncodingError after the response headers,+ # past _CONVEX_RETRY's reach (urllib3 only retries pre-response failures). The reads are+ # idempotent GETs, so a fresh request must re-fetch the page rather than fail the sync.+ manager = _make_manager(can_resume=False)+ mock_get.return_value.get.side_effect = [+ ChunkedEncodingError("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)"),+ _make_response({"values": [{"_id": "a"}], "cursor": 30, "hasMore": False}),+ ]++ # tenacity attaches the Retrying controller as `.retry`; stub its sleep so the backoff+ # between attempts doesn't actually block the test.+ with patch.object(_convex_get.retry, "sleep"): # type: ignore[attr-defined]+ batches = list(document_deltas("https://x.convex.cloud", "key", "t", 10, manager))++ assert batches == [[{"_id": "a"}]]+ assert mock_get.return_value.get.call_count == 2++class TestConvexRetryPolicy:@parameterized.expand([