: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-imports): purge stray delta log files safely on unrecoverable table wipe (#76190)
Tom Owersa377d0ef67d8
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.pyindex b5a468df1300..86ebcf34993d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.py@@ -206,8 +206,14 @@ class DeltaTableHelper:await self._logger.aerror(f"get_delta_table: deleting unrecoverable delta table for a fresh sync: {error_text}")- async with aget_s3_client() as s3:- await s3._rm(delta_uri, recursive=True)+ # A bare recursive `_rm` can leave `_delta_log` strays behind on S3-compatible+ # stores (see `_purge_s3_prefix_once`), which would recreate this exact+ # "No table metadata or protocol found" corruption on the very next sync.+ async with aget_s3_client(fresh_instance=True) as s3:+ try:+ await _purge_s3_prefix(s3, delta_uri)+ except FileNotFoundError:+ passelse:raisediff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.pyindex f744eb1f5cfb..c542e63c1beb 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.py@@ -117,16 +117,17 @@ class TestGetDeltaTableUnrecoverableErrors:delta_uri = "s3://bucket/team_id/job_id/t"s3 = MagicMock()- s3._rm = AsyncMock()s3_cm = MagicMock()s3_cm.__aenter__ = AsyncMock(return_value=s3)s3_cm.__aexit__ = AsyncMock(return_value=False)+ mock_aget_s3_client = MagicMock(return_value=s3_cm)module = "products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta_table_helper"with (patch.object(helper, "_get_delta_table_uri", AsyncMock(return_value=delta_uri)),patch(f"{module}.deltalake.DeltaTable") as mock_delta_table,- patch(f"{module}.aget_s3_client", MagicMock(return_value=s3_cm)),+ patch(f"{module}.aget_s3_client", mock_aget_s3_client),+ patch(f"{module}._purge_s3_prefix", AsyncMock()) as mock_purge,patch(f"{module}.capture_exception"),):mock_delta_table.is_deltatable.return_value = True@@ -136,13 +137,44 @@ class TestGetDeltaTableUnrecoverableErrors:result = await helper.get_delta_table()assert result is Noneassert helper.is_first_sync is True- s3._rm.assert_awaited_once_with(delta_uri, recursive=True)+ # Regression guard: a bare recursive `_rm` (instead of the enumerate-then-delete+ # `_purge_s3_prefix`) can leave `_delta_log` strays on S3-compatible stores and+ # recreate this exact corruption on the next sync.+ mock_purge.assert_awaited_once_with(s3, delta_uri)+ mock_aget_s3_client.assert_called_once_with(fresh_instance=True)else:with pytest.raises(Exception, match="something else went wrong"):await helper.get_delta_table()- s3._rm.assert_not_awaited()+ mock_purge.assert_not_awaited()assert helper.is_first_sync is False+ @pytest.mark.asyncio+ async def test_open_failure_heals_even_if_prefix_already_gone(self):+ """A concurrent purge (e.g. a retried Temporal attempt) can already have cleared the+ prefix by the time this one runs `_purge_s3_prefix`, which surfaces as `FileNotFoundError`.+ That's still a successful heal, not a failure to propagate."""+ helper = DeltaTableHelper(resource_name="t", job=MagicMock(), logger=_make_logger())+ delta_uri = "s3://bucket/team_id/job_id/t"++ s3_cm = MagicMock()+ s3_cm.__aenter__ = AsyncMock(return_value=MagicMock())+ s3_cm.__aexit__ = AsyncMock(return_value=False)++ module = "products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta_table_helper"+ with (+ patch.object(helper, "_get_delta_table_uri", AsyncMock(return_value=delta_uri)),+ patch(f"{module}.deltalake.DeltaTable") as mock_delta_table,+ patch(f"{module}.aget_s3_client", MagicMock(return_value=s3_cm)),+ patch(f"{module}._purge_s3_prefix", AsyncMock(side_effect=FileNotFoundError)),+ patch(f"{module}.capture_exception"),+ ):+ mock_delta_table.is_deltatable.return_value = True+ mock_delta_table.side_effect = Exception("Kernel error: No table metadata or protocol found in delta log.")++ result = await helper.get_delta_table()+ assert result is None+ assert helper.is_first_sync is True+@pytest.mark.asyncioasync def test_is_deltatable_failure_is_captured_and_reraised(self):"""The `is_deltatable` existence check is a separate S3 call from the DeltaTable() open