diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 5d3cfcd832..53d0ffda75 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -2329,7 +2329,8 @@ def run(self) -> T: # ConnectionFailures do not supply a code property exc_code = getattr(exc, "code", None) if self._is_not_eligible_for_retry() or ( - exc_code and exc_code not in helpers._RETRYABLE_ERROR_CODES + isinstance(exc, OperationFailure) + and exc_code not in helpers._RETRYABLE_ERROR_CODES ): raise self._retrying = True diff --git a/test/mockupdb/test_cursor.py b/test/mockupdb/test_cursor.py index 1cf3a05ed5..96a7e17053 100644 --- a/test/mockupdb/test_cursor.py +++ b/test/mockupdb/test_cursor.py @@ -16,11 +16,13 @@ from __future__ import annotations import unittest +from test import PyMongoTestCase from mockupdb import MockupDB, OpMsg, going from bson.objectid import ObjectId from pymongo import MongoClient +from pymongo.errors import OperationFailure class TestCursor(unittest.TestCase): @@ -57,5 +59,31 @@ def test_getmore_load_balanced(self): request.replies({"cursor": {"id": cursor_id, "nextBatch": [{}]}}) +class TestRetryableErrorCodeCatch(PyMongoTestCase): + def _test_fail_on_operation_failure_with_code(self, code): + """Test reads on error codes that should not be retried""" + server = MockupDB() + server.run() + self.addCleanup(server.stop) + server.autoresponds("ismaster", maxWireVersion=6) + + client = MongoClient(server.uri) + + with going(lambda: server.receives(OpMsg({"find": "collection"})).command_err(code=code)): + cursor = client.db.collection.find() + with self.assertRaises(OperationFailure) as ctx: + cursor.next() + self.assertEqual(ctx.exception.code, code) + + def test_fail_on_operation_failure_none(self): + self._test_fail_on_operation_failure_with_code(None) + + def test_fail_on_operation_failure_zero(self): + self._test_fail_on_operation_failure_with_code(0) + + def test_fail_on_operation_failure_one(self): + self._test_fail_on_operation_failure_with_code(1) + + if __name__ == "__main__": unittest.main()