: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(mongodb): classify KeyNotFound (code 211) as non-retryable (#93076)
Tom Owersfe5ac58f01b3
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.pyindex 0170288f0561..79ee009fe836 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py@@ -78,6 +78,17 @@ _MONGO_CONNECT_FAILED_MESSAGE = ("Could not connect to your MongoDB database. Check your connection string and credentials, then try again.")+# MongoDB error code 211 (KeyNotFound): the cluster's internal keystore has no HMAC key valid for+# the requested timestamp. This is a cluster-side key management problem (e.g. key rotation+# removed keys the importer needed). Retrying reads the same cursor against the same cluster and+# always fails with the same error, so it is non-retryable.+_MONGO_KEY_NOT_FOUND_MESSAGE = (+ "PostHog couldn't sync this MongoDB collection because your cluster reported a key management "+ "error (MongoDB code 211: KeyNotFound). Your cluster's keystore does not have a valid HMAC key "+ "for the time range being queried. Check your MongoDB cluster's key management configuration "+ "and ensure all replica set members are healthy, then try again."+)+# Connection succeeded but nothing importable came back. This is usually a wrong-database or# permission problem rather than a genuinely empty database: a connection string ending in /admin# or /test lands on an empty system database, and a user without read access sees no collections.@@ -167,6 +178,12 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixin# differently (a bare AutoReconnect / NetworkTimeout with no topology description) and# stays retryable."Topology Description:": _MONGO_UNREACHABLE_MESSAGE,+ # MongoDB OperationFailure code 211 (KeyNotFound): the cluster's HMAC keystore has no+ # valid key for the cursor's timestamp. pymongo formats the full server error response+ # as part of the exception message; the leading phrase before the variable parts+ # (timestamp, key id) is stable and unambiguous. Retrying the same cursor against the+ # same cluster always fails with the same error.+ "No keys found for HMAC": _MONGO_KEY_NOT_FOUND_MESSAGE,}def get_retryable_errors(self) -> set[str]:diff --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 3e3ca2f592bd..c96dc242fe69 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@@ -429,6 +429,16 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):"('atlas-sql-681905984ce3f87167df11fa-wf3cgp.a.query.mongodb.net', 27017) ""server_type: Unknown, rtt: None, error=AutoReconnect('...connection closed...')>]>",),+ # MongoDB OperationFailure code 211 (KeyNotFound): the cluster's HMAC keystore has no+ # valid key for the cursor's timestamp. Retrying the same cursor always fails the same+ # way, so it must be classified non-retryable.+ (+ "key_not_found_hmac",+ "No keys found for HMAC that is valid for time: { ts: Timestamp(1000000000, 1) } "+ "with id: 1234567890, full error: {'ok': 0.0, 'errmsg': 'No keys found for HMAC "+ "that is valid for time: { ts: Timestamp(1000000000, 1) } with id: 1234567890', "+ "'code': 211, 'codeName': 'KeyNotFound'}",+ ),])def test_known_errors_are_non_retryable(self, _name, error_msg):@@ -464,6 +474,7 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):("atlas_sql_endpoint", "query.mongodb.net", "connection string"),("unescaped_credentials", "must be escaped according to RFC 3986", "connection string"),("document_missing_id", "one of its documents has no _id field", "view"),+ ("key_not_found", "No keys found for HMAC", "key management"),])def test_pattern_has_friendly_message(self, _name, pattern, expected_substring):