From f84d00335347cdb3ab0b11164f91a5373952dfb5 Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Thu, 27 Aug 2026 10:05:48 -0400 Subject: [PATCH] PYTHON-5300 - Fix unraisable error failure for test_infinite_loop_exceeds_max_recursion_depth --- test/asynchronous/test_custom_types.py | 13 ++++++++++--- test/test_custom_types.py | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/test/asynchronous/test_custom_types.py b/test/asynchronous/test_custom_types.py index c0564dd876..f5f5f5d8c4 100644 --- a/test/asynchronous/test_custom_types.py +++ b/test/asynchronous/test_custom_types.py @@ -17,6 +17,7 @@ from __future__ import annotations import datetime +import gc import sys import tempfile from collections import OrderedDict @@ -427,9 +428,15 @@ def test_infinite_loop_exceeds_max_recursion_depth(self): type_registry=TypeRegistry([self.B2A()], fallback_encoder=self.fallback_encoder_A2B) ) - # Raises max recursion depth exceeded error - with self.assertRaises(RuntimeError): - encode({"x": self.TypeA(100)}, codec_options=codecopts) + # GC while the stack is exhausted may trigger an unraisable RecursionError that causes a failure + gc.collect() + gc.disable() + try: + # Raises max recursion depth exceeded error + with self.assertRaises(RuntimeError): + encode({"x": self.TypeA(100)}, codec_options=codecopts) + finally: + gc.enable() class TestTypeRegistry(unittest.TestCase): diff --git a/test/test_custom_types.py b/test/test_custom_types.py index b55eb64d9d..c26ad917bc 100644 --- a/test/test_custom_types.py +++ b/test/test_custom_types.py @@ -17,6 +17,7 @@ from __future__ import annotations import datetime +import gc import sys import tempfile from collections import OrderedDict @@ -427,9 +428,15 @@ def test_infinite_loop_exceeds_max_recursion_depth(self): type_registry=TypeRegistry([self.B2A()], fallback_encoder=self.fallback_encoder_A2B) ) - # Raises max recursion depth exceeded error - with self.assertRaises(RuntimeError): - encode({"x": self.TypeA(100)}, codec_options=codecopts) + # GC while the stack is exhausted may trigger an unraisable RecursionError that causes a failure + gc.collect() + gc.disable() + try: + # Raises max recursion depth exceeded error + with self.assertRaises(RuntimeError): + encode({"x": self.TypeA(100)}, codec_options=codecopts) + finally: + gc.enable() class TestTypeRegistry(unittest.TestCase):