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):