: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(exports): degrade gracefully when cohort is deleted mid-export (#65327)
posthog[bot]c5a5a3a4a88b
diff --git a/products/exports/backend/tasks/csv_exporter.py b/products/exports/backend/tasks/csv_exporter.pyindex 33445fa5f054..141da37a4fd9 100644--- a/products/exports/backend/tasks/csv_exporter.py+++ b/products/exports/backend/tasks/csv_exporter.py@@ -412,6 +412,20 @@ def get_from_insights_api(exported_asset: ExportedAsset, limit: int, resource: dtry:response = make_api_call(access_token, body, limit, method, next_url, path)except HTTPError as e:+ # The underlying resource (e.g. a cohort) can be deleted or become unresolvable+ # mid-export, which surfaces as a 404 partway through pagination. Treat that as+ # end-of-data and return what we have rather than failing the whole export.+ if e.response is not None and e.response.status_code == 404:+ logger.warning(+ "csv_exporter.resource_gone",+ exc=e,+ exc_info=True,+ path=path,+ next_url=next_url,+ total=total,+ )+ break+if "Query size exceeded" not in e.response.text:raisediff --git a/products/exports/backend/tasks/test/test_csv_exporter.py b/products/exports/backend/tasks/test/test_csv_exporter.pyindex b4ac7a908434..ae8dd90310b2 100644--- a/products/exports/backend/tasks/test/test_csv_exporter.py+++ b/products/exports/backend/tasks/test/test_csv_exporter.py@@ -360,6 +360,47 @@ class TestCSVExporter(APIBaseTest):assert patched_make_api_call.call_count == 4patched_make_api_call.assert_called_with(mock.ANY, mock.ANY, 64, mock.ANY, mock.ANY, mock.ANY)+ @patch("products.exports.backend.tasks.csv_exporter.logger")+ def test_404_mid_export_returns_rows_already_fetched(self, _mock_logger: MagicMock) -> None:+ # A cohort can be deleted mid-export, surfacing as a 404 partway through pagination.+ # We should return what we already fetched rather than failing the whole export.+ with patch("products.exports.backend.tasks.csv_exporter.make_api_call") as patched_make_api_call:+ exported_asset = self._create_asset()++ first_page = Mock()+ first_page.json.return_value = {+ "next": "http://testserver/api/cohort/1/persons?offset=512",+ "results": [{"id": "abc", "distinct_id": "1"}],+ }++ not_found_error = HTTPError("404 Client Error") # type: ignore[call-arg]+ not_found_error.response = Mock()+ not_found_error.response.status_code = 404+ not_found_error.response.text = "Not found."++ patched_make_api_call.side_effect = [first_page, not_found_error]++ with self.settings(OBJECT_STORAGE_ENABLED=False):+ csv_exporter.export_tabular(exported_asset)++ assert patched_make_api_call.call_count == 2+ assert exported_asset.content is not None+ assert b"abc" in exported_asset.content++ @patch("products.exports.backend.tasks.csv_exporter.logger")+ def test_non_404_http_error_still_raises(self, _mock_logger: MagicMock) -> None:+ with patch("products.exports.backend.tasks.csv_exporter.make_api_call") as patched_make_api_call:+ exported_asset = self._create_asset()++ server_error = HTTPError("500 Server Error") # type: ignore[call-arg]+ server_error.response = Mock()+ server_error.response.status_code = 500+ server_error.response.text = "Internal server error"+ patched_make_api_call.side_effect = server_error++ with pytest.raises(HTTPError):+ csv_exporter.export_tabular(exported_asset)+def test_limiting_query_as_expected(self) -> None:with self.settings(SITE_URL="https://app.posthog.com"):modified_url = add_query_params(absolute_uri(regression_11204), {"limit": "3500"})