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): skip webhook files already consumed by another run (#80206)

Tom Owersccd195ca8263
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.py
index 68b5b75c298d..d992b0422af3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.py
@@ -347,6 +347,42 @@ class TestWebhookSourceManager:
assert tables[0].column_names == ["id"]
transformer.assert_called_once()
+ async def test_get_items_skips_file_deleted_before_read(self):
+ schema_id = "test-schema"
+ manager = _make_manager(team_id=1, schema_id=schema_id)
+
+ payloads = [{"id": "2", "value": "b"}]
+ parquet_bytes = _make_webhook_parquet_bytes(payloads, team_id=1, schema_id=schema_id)
+
+ mock_file = AsyncMock()
+ mock_file.read = AsyncMock(return_value=parquet_bytes)
+ mock_s3 = AsyncMock()
+ # First file's read raises as if a concurrent run already consumed and deleted it;
+ # the second file still yields normally.
+ first_open = AsyncMock()
+ first_open.__aenter__ = AsyncMock(side_effect=FileNotFoundError("The specified key does not exist."))
+ first_open.__aexit__ = AsyncMock(return_value=False)
+ second_open = AsyncMock()
+ second_open.__aenter__ = AsyncMock(return_value=mock_file)
+ second_open.__aexit__ = AsyncMock(return_value=False)
+ mock_s3.open_async = AsyncMock(side_effect=[first_open, second_open])
+ mock_s3._rm = AsyncMock()
+
+ with (
+ patch.object(
+ manager,
+ "_list_webhook_parquet_files",
+ return_value=["s3://bucket/gone.parquet", "s3://bucket/file.parquet"],
+ ),
+ _mock_s3_context(mock_s3),
+ ):
+ tables = [table async for table in manager.get_items()]
+
+ assert len(tables) == 1
+ assert tables[0].column("id").to_pylist() == ["2"]
+ # Only the successfully read file is removed; the vanished one was never touched.
+ mock_s3._rm.assert_awaited_once_with("bucket/file.parquet")
+
async def test_get_items_yields_nothing_when_no_files(self):
manager = _make_manager()
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py
index e3573156abc0..cebaf8bd0ab4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py
@@ -154,9 +154,16 @@ class WebhookSourceManager:
path = self._strip_s3_protocol(file)
await self._logger.adebug(f"Webhook source reading file {path}")
- async with await s3.open_async(path, "rb") as f:
- data = await f.read()
- table = pq.read_table(pa.BufferReader(data))
+ try:
+ async with await s3.open_async(path, "rb") as f:
+ data = await f.read()
+ table = pq.read_table(pa.BufferReader(data))
+ except FileNotFoundError:
+ # A concurrent run (or a retry of this same activity) can have already
+ # read and deleted this file between our listing and this open, since
+ # this is a plain listing snapshot, not a lease on the listed files.
+ await self._logger.adebug("webhook_file_already_consumed", path=path)
+ continue
table = await self._validate_webhook_table(table)
if table.num_rows == 0: