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(data-imports): retry a transient S3 permission blip during table reset (#84381)

Tom Owers4499c9b38408
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/table.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/table.py
index 7812f3af134e..86f5d0ab6f51 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/table.py
@@ -27,6 +27,23 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.del
_PURGE_S3_PREFIX_MAX_ATTEMPTS = 4
+def _is_retryable_purge_error(error: OSError) -> bool:
+ """True for an error worth retrying `_purge_s3_prefix` on.
+
+ Covers the known-transient object-store blips (see is_transient_object_store_error) plus a bare
+ `PermissionError`: s3fs translates every S3 auth-failure response code (AccessDenied,
+ ExpiredToken, InvalidAccessKeyId, ...) into this one exception type, and a HeadObject 403 never
+ carries the underlying code in its body (AWS omits it for HEAD requests), so a transient
+ credential-resolution race can't be told apart from a genuine permission problem by message here.
+ `_purge_s3_prefix` always runs against a freshly created client (aget_s3_client(fresh_instance=True)),
+ which re-resolves credentials on every call — the same IMDS/STS race already covered for
+ NoCredentialsError above, just surfacing as an explicit S3-side denial instead of a local
+ resolution failure. Retrying the same bounded budget lets that race self-heal; a persistent
+ misconfiguration still raises once the budget is exhausted, since this only defers the error.
+ """
+ return is_transient_object_store_error(error) or isinstance(error, PermissionError)
+
+
async def _purge_s3_prefix(s3: Any, uri: str) -> None:
"""Delete every object under `uri`, retrying on transient S3 SlowDown throttling.
@@ -40,7 +57,7 @@ async def _purge_s3_prefix(s3: Any, uri: str) -> None:
return
except OSError as e:
attempt += 1
- if attempt >= _PURGE_S3_PREFIX_MAX_ATTEMPTS or not is_transient_object_store_error(e):
+ if attempt >= _PURGE_S3_PREFIX_MAX_ATTEMPTS or not _is_retryable_purge_error(e):
raise
await asyncio.sleep(2**attempt)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_table.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_table.py
index 4d9c79cdd9c0..5a9ec33e6136 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_table.py
@@ -11,7 +11,11 @@ from parameterized import parameterized
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.errors import (
TransientObjectStoreError,
)
-from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.table import DeltaTableRef
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.table import (
+ _PURGE_S3_PREFIX_MAX_ATTEMPTS,
+ DeltaTableRef,
+ _purge_s3_prefix,
+)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.test.helpers import make_logger
@@ -294,3 +298,39 @@ class TestIsTableCorrupted:
result = await table_ref.is_table_corrupted()
assert result is expected
+
+
+class TestPurgeS3PrefixRetriesPermissionError:
+ _MODULE = "products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.table"
+
+ # A HeadObject 403 never carries the underlying S3 error code in its body (AWS omits it for HEAD
+ # requests), so a fresh client's brief IMDS/STS credential-resolution race surfaces identically to a
+ # genuine permission problem: a bare PermissionError("Forbidden"). Before this fix, `_purge_s3_prefix`
+ # only retried the needles in `is_transient_object_store_error` and raised immediately on any
+ # PermissionError, failing the whole sync for what was actually a transient, self-healing race.
+ @pytest.mark.asyncio
+ async def test_retries_permission_error_and_succeeds_once_it_clears(self):
+ with (
+ patch(
+ f"{self._MODULE}._purge_s3_prefix_once",
+ new=AsyncMock(side_effect=[PermissionError("Forbidden"), None]),
+ ) as mock_once,
+ patch(f"{self._MODULE}.asyncio.sleep", new=AsyncMock()),
+ ):
+ await _purge_s3_prefix(MagicMock(), "s3://bucket/prefix")
+
+ assert mock_once.await_count == 2
+
+ @pytest.mark.asyncio
+ async def test_gives_up_after_max_attempts_on_persistent_permission_error(self):
+ with (
+ patch(
+ f"{self._MODULE}._purge_s3_prefix_once",
+ new=AsyncMock(side_effect=PermissionError("Forbidden")),
+ ) as mock_once,
+ patch(f"{self._MODULE}.asyncio.sleep", new=AsyncMock()),
+ ):
+ with pytest.raises(PermissionError):
+ await _purge_s3_prefix(MagicMock(), "s3://bucket/prefix")
+
+ assert mock_once.await_count == _PURGE_S3_PREFIX_MAX_ATTEMPTS