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(stripe): drain all split batcher chunks before batching next item (#66493)

Tom Owers87953d7da9eb
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
index bb6d1850c98c..d9e3f22e3098 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
@@ -413,7 +413,10 @@ def get_rows(
}
)
- if batcher.should_yield():
+ # A single batch can split into several ready chunks, so drain them all
+ # before batching the next item — otherwise the next batch() trips the
+ # "table already ready" guard.
+ while batcher.should_yield():
py_table = batcher.get_table()
yield py_table
@@ -437,14 +440,14 @@ def get_rows(
for obj in stripe_objects.auto_paging_iter():
batcher.batch(obj)
- if batcher.should_yield():
+ while batcher.should_yield():
py_table = batcher.get_table()
yield py_table
last_cur = py_table.column("id")[-1].as_py()
resumable_source_manager.save_state(StripeResumeConfig(starting_after=last_cur))
- if batcher.should_yield(include_incomplete_chunk=True):
+ while batcher.should_yield(include_incomplete_chunk=True):
py_table = batcher.get_table()
yield py_table
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
index ce650e9a6773..368f6f5d73f9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -1,3 +1,4 @@
+import functools
from typing import Any, cast
import pytest
@@ -348,6 +349,90 @@ class TestSubscriptionPageSize:
assert captured["limit"] == SUBSCRIPTION_PAGE_LIMIT
+class TestStripeBatcherDrainsSplitChunks:
+ def test_flat_resource_drains_all_split_chunks_per_batch(self):
+ # A single batch() can split a flushed buffer into several ready chunks (large rows over the
+ # per-table byte cap). get_rows must drain every chunk before batching the next object,
+ # otherwise the following batch() raises "Batcher already has a table ready to yield."
+ objects = [{"id": f"ch_{i}", "description": "x" * 10} for i in range(6)]
+ resource = StripeResource(method=lambda params: cast(ListObject[Any], _FakeStripeList(objects)))
+
+ resumable_source_manager = MagicMock()
+ resumable_source_manager.can_resume.return_value = False
+
+ # Tiny caps force every 2-row buffer flush to split into single-row chunks.
+ splitting_batcher = functools.partial(
+ stripe_module.Batcher, chunk_size=2, max_table_bytes=1, max_column_offset_bytes=1
+ )
+
+ with (
+ patch.object(stripe_module, "StripeClient"),
+ patch.object(stripe_module, "Batcher", splitting_batcher),
+ patch.object(stripe_module, "_build_resources", return_value={"charge": resource}),
+ ):
+ rows: list[dict] = []
+ for table in get_rows(
+ api_key="sk_test_123",
+ endpoint="charge",
+ account_id=None,
+ db_incremental_field_last_value=None,
+ db_incremental_field_earliest_value=None,
+ logger=MagicMock(),
+ resumable_source_manager=resumable_source_manager,
+ ):
+ rows.extend(table.to_pylist())
+
+ assert [row["id"] for row in rows] == [f"ch_{i}" for i in range(6)]
+
+ def test_nested_resource_drains_all_split_chunks_per_batch(self):
+ nested_objects = [{"id": f"cbt_{i}", "amount": 100, "note": "y" * 10} for i in range(6)]
+
+ def nested_method(customer=None, params=None):
+ return _list_object(nested_objects)
+
+ splitting_batcher = functools.partial(
+ stripe_module.Batcher, chunk_size=2, max_table_bytes=1, max_column_offset_bytes=1
+ )
+
+ with patch.object(stripe_module, "Batcher", splitting_batcher):
+ rows = _run_nested_get_rows(nested_method, parent_objects=[{"id": "cus_1"}])
+
+ assert [row["id"] for row in rows] == [f"cbt_{i}" for i in range(6)]
+
+ def test_final_incomplete_chunk_drain_splits(self):
+ # The final drain takes a different path: get_table() flushes the leftover buffer and can
+ # itself produce multiple chunks. A large chunk_size keeps every row in the buffer until the
+ # end, so all rows go through that final drain — which must drain every split chunk.
+ objects = [{"id": f"ch_{i}", "description": "z" * 10} for i in range(4)]
+ resource = StripeResource(method=lambda params: cast(ListObject[Any], _FakeStripeList(objects)))
+
+ resumable_source_manager = MagicMock()
+ resumable_source_manager.can_resume.return_value = False
+
+ splitting_batcher = functools.partial(
+ stripe_module.Batcher, chunk_size=100, max_table_bytes=1, max_column_offset_bytes=1
+ )
+
+ with (
+ patch.object(stripe_module, "StripeClient"),
+ patch.object(stripe_module, "Batcher", splitting_batcher),
+ patch.object(stripe_module, "_build_resources", return_value={"charge": resource}),
+ ):
+ rows: list[dict] = []
+ for table in get_rows(
+ api_key="sk_test_123",
+ endpoint="charge",
+ account_id=None,
+ db_incremental_field_last_value=None,
+ db_incremental_field_earliest_value=None,
+ logger=MagicMock(),
+ resumable_source_manager=resumable_source_manager,
+ ):
+ rows.extend(table.to_pylist())
+
+ assert [row["id"] for row in rows] == [f"ch_{i}" for i in range(4)]
+
+
class TestCustomerMightHaveBalanceTransactions:
@pytest.mark.parametrize(
"customer,expected",