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(warehouse_sources): restart Notion page-id enumeration on stale search cursor (#90205)

Tom Owers286f9189484c
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/notion/notion.py b/products/warehouse_sources/backend/temporal/data_imports/sources/notion/notion.py
index 321be311bc3e..19cc0108aae7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/notion/notion.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/notion/notion.py
@@ -345,9 +345,19 @@ def _iter_page_ids(
) -> Iterator[str]:
cursor: str | None = None
while True:
- data = _request(
- session, "POST", "/v1/search", logger, json_body=_search_body("page", cursor), throttle=throttle
- )
+ try:
+ data = _request(
+ session, "POST", "/v1/search", logger, json_body=_search_body("page", cursor), throttle=throttle
+ )
+ except NotionBadRequestError as e:
+ if cursor is None or not _is_invalid_start_cursor(e):
+ raise
+ # A cursor can expire mid-enumeration on a large workspace; restart from the beginning
+ # rather than failing the blocks/comments fan-out. Page ids already yielded get re-yielded,
+ # which the callers tolerate (blocks/comments dedup on primary key at merge).
+ logger.warning("Notion: page-id search cursor rejected as invalid; restarting enumeration from the start")
+ cursor = None
+ continue
for item in data.get("results", []):
# "id" is the primary key driving the blocks/comments fan-out; access it directly so a
# malformed response missing it surfaces loudly instead of silently dropping the page.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/notion/tests/test_notion.py b/products/warehouse_sources/backend/temporal/data_imports/sources/notion/tests/test_notion.py
index e6be2caf0e8f..c6f8028b8e3f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/notion/tests/test_notion.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/notion/tests/test_notion.py
@@ -22,6 +22,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.notion.not
_comments_stream,
_get_headers,
_iter_block_children,
+ _iter_page_ids,
_parse_retry_after,
_request,
_search_body,
@@ -213,6 +214,37 @@ class TestNotion:
assert session.calls[0]["params"]["start_cursor"] == "stale-cursor"
assert "start_cursor" not in session.calls[1]["params"]
+ def test_iter_page_ids_restarts_when_cursor_invalid(self) -> None:
+ # A page-id search cursor can expire mid-enumeration on a large workspace, which Notion
+ # rejects with the same 400 validation_error as the search/users streams. The blocks/comments
+ # fan-out must restart enumeration rather than crashing the whole sync.
+ session = FakeSession(
+ [
+ _list_response([{"id": "p1"}], has_more=True, next_cursor="c1"),
+ self._invalid_cursor_response(),
+ _list_response([{"id": "p1"}], has_more=False, next_cursor=None),
+ ]
+ )
+ logger = mock.MagicMock()
+
+ page_ids = list(_iter_page_ids(cast(requests.Session, session), logger))
+
+ assert page_ids == ["p1", "p1"]
+ assert logger.warning.called
+ # Second request replays the now-stale cursor (rejected); the restart carries no cursor.
+ assert session.calls[1]["json"]["start_cursor"] == "c1"
+ assert "start_cursor" not in session.calls[2]["json"]
+
+ def test_iter_page_ids_propagates_non_cursor_bad_request(self) -> None:
+ # A 400 that is not the invalid-cursor case is a genuine bad request and must still fail the
+ # sync rather than being silently restarted.
+ other_400 = FakeResponse({}, status_code=400)
+ other_400.text = '{"code":"validation_error","message":"something else"}'
+ session = FakeSession([other_400])
+
+ with pytest.raises(NotionBadRequestError):
+ list(_iter_page_ids(cast(requests.Session, session), mock.MagicMock()))
+
def test_block_children_inject_page_id(self) -> None:
session = FakeSession([_list_response([{"id": "b1", "has_children": False}], has_more=False, next_cursor=None)])
blocks = list(