: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(warehouse-sources): resume mongodb sync after a getMore hits the execution time limit (#101013)
Tom Owersb03f828d8885
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.pyindex 22fdf7c11e99..017e93ae2a20 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.py@@ -639,6 +639,15 @@ MONGO_KEYS_UNAVAILABLE_ERROR = ("unavailable. This clears by itself, and the sync will run again automatically.")+# MongoDB OperationFailure code 50 (MaxTimeMSExpired / pymongo's ExecutionTimeout): the server killed+# a getMore because it ran past an execution time limit. We never set maxTimeMS ourselves on this+# query, so this is the limit enforced by the cluster itself — notably Atlas free/shared/flex tiers,+# which cap total operation time regardless of client options (the same tiers already special-cased+# above for rejecting no_cursor_timeout). The cursor is _id-ordered, so last_id is a safe resume point+# exactly as for CursorNotFound; a getMore killed before yielding anything would hit the identical+# limit on retry, so that case re-raises instead of looping forever.+_EXECUTION_TIMEOUT_ERROR_CODE = 50+def mongo_source(connection_string: str,@@ -766,6 +775,18 @@ def mongo_source(except OperationFailure as e:if e.code == _KEY_NOT_FOUND_ERROR_CODE:raise OperationFailure(MONGO_KEYS_UNAVAILABLE_ERROR, e.code) from e+ if e.code == _EXECUTION_TIMEOUT_ERROR_CODE:+ if rows_since_cursor_opened == 0:+ raise+ logger.debug(+ f"MongoDB: operation exceeded time limit for collection={collection_name}; "+ f"resuming after _id={last_id}"+ )+ cursor.close()+ cursor = open_resumable_cursor()+ rows_since_cursor_opened = 0+ continue+# The option is rejected when the cursor is opened, before any document is# yielded, so retrying without it can't duplicate rows. The tradeoff is that# the server-side idle timeout applies again — hence the CursorNotFounddiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.pyindex 362e2d1354dd..dda8c7e0e815 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.py@@ -766,6 +766,35 @@ class TestMongoSourceCursorLifecycle(SimpleTestCase):assert collection.cursors[0].sorted_by == ["_id", 1]assert collection.cursors[1].sorted_by == ["_id", 1]+ def test_execution_timeout_mid_stream_resumes_from_last_id(self):+ # Regression: Atlas free/shared/flex tier clusters enforce a hard operation execution-time+ # limit independent of no_cursor_timeout, so a getMore against a large collection can be+ # killed with OperationFailure code 50 (MaxTimeMSExpired) partway through. The cursor is+ # _id-ordered, so last_id is a safe resume point — resume instead of failing the whole sync.+ collection = _FakeCollection(+ [{"_id": "1"}, {"_id": "2"}, {"_id": "3"}],+ error=OperationFailure("operation exceeded time limit, correlationID = 18d582877a9d2284f38efbb3", 50),+ error_after=2,+ fallback_docs=[{"_id": "1"}, {"_id": "2"}, {"_id": "3"}],+ )++ rows = self._run_get_rows(collection)++ assert [row["_id"] for row in rows] == ["1", "2", "3"]+ assert len(collection.find_calls) == 2+ # Resume query picks up after the last document that was yielded.+ assert collection.find_queries[1] == {"_id": {"$gt": "2"}}++ def test_execution_timeout_without_progress_is_not_retried_forever(self):+ # Resuming re-runs the same query, so a getMore that times out before yielding anything+ # would hit the identical limit again — it must re-raise instead of looping forever.+ collection = _FakeCollection([], error=OperationFailure("operation exceeded time limit", 50))++ with self.assertRaises(OperationFailure):+ self._run_get_rows(collection)++ assert len(collection.find_calls) == 1+@parameterized.expand([(