From 3dbc9a240408870e7a41866f9db282d9cee57474 Mon Sep 17 00:00:00 2001 From: Roman Golev <67314258+romangolev@users.noreply.github.com> Date: Fri, 4 Dec 2026 22:58:58 +0100 Subject: [PATCH 1/2] Keep the recursion check on light-throw targets Light-throw targets compiled after a recursion limit was set skipped the check. Refs #1611. --- src/core/IronPython/Runtime/FunctionCode.cs | 2 +- .../suite/modules/system_related/test_sys.py | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/core/IronPython/Runtime/FunctionCode.cs b/src/core/IronPython/Runtime/FunctionCode.cs index c4e38eb98..775e875d6 100644 --- a/src/core/IronPython/Runtime/FunctionCode.cs +++ b/src/core/IronPython/Runtime/FunctionCode.cs @@ -201,7 +201,7 @@ internal void LightThrowCompile(CodeContext/*!*/ context) { lock (pyCtx._codeUpdateLock) { if (context.LanguageContext.EnableTracing == enableTracing) { - LightThrowTarget = target; + LightThrowTarget = _lambda is Compiler.Ast.FunctionDefinition ? AddRecursionCheck(pyCtx, target) : target; } } } catch (InvalidOperationException ex) { diff --git a/tests/suite/modules/system_related/test_sys.py b/tests/suite/modules/system_related/test_sys.py index c1a44e820..944e56c98 100644 --- a/tests/suite/modules/system_related/test_sys.py +++ b/tests/suite/modules/system_related/test_sys.py @@ -702,6 +702,46 @@ def f(n): print(False) except RecursionError: print(True) +""" + out = subprocess.check_output([sys.executable, '-c', code]) + self.assertEqual(out.strip(), b'True') + + def test_setrecursionlimit_light_throw_target(self): + import subprocess + code = """import sys +import time +from System.Threading import Thread, ThreadStart + +def f(n, boom): + try: + if n == 0: + if boom: + raise ZeroDivisionError + return 0 + return f(n - 1, boom) + except KeyError: + return -1 + +sys.setrecursionlimit(200) +for _ in range(40): + try: + f(2, True) + except ZeroDivisionError: + pass +time.sleep(3) + +out = [] +def run(): + try: + eval("f(1000000, False)") + out.append(False) + except RecursionError: + out.append(True) + +t = Thread(ThreadStart(run), 256 * 1024) +t.Start() +t.Join() +print(out[0]) """ out = subprocess.check_output([sys.executable, '-c', code]) self.assertEqual(out.strip(), b'True') From 4ee2f210b0953487521c86bbeff41ca64b0409a0 Mon Sep 17 00:00:00 2001 From: Roman Golev <67314258+romangolev@users.noreply.github.com> Date: Sat, 5 Dec 2026 01:40:19 +0100 Subject: [PATCH 2/2] Raise RecursionError instead of overflowing a nearly exhausted stack With a recursion limit set, check the real stack in FunctionPushFrame so deep calls on small-stack threads raise RecursionError rather than crash. --- .../Runtime/Operations/PythonOps.cs | 5 +++++ .../suite/modules/system_related/test_sys.py | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/core/IronPython/Runtime/Operations/PythonOps.cs b/src/core/IronPython/Runtime/Operations/PythonOps.cs index 40f9e8d6c..b1a74de86 100644 --- a/src/core/IronPython/Runtime/Operations/PythonOps.cs +++ b/src/core/IronPython/Runtime/Operations/PythonOps.cs @@ -3506,6 +3506,11 @@ public static void FunctionPushFrame(PythonContext context) { if (PythonFunction.AddRecursionDepth(1) > context.RecursionLimit) { throw PythonOps.RecursionError("maximum recursion depth exceeded"); } + try { + RuntimeHelpers.EnsureSufficientExecutionStack(); + } catch (InsufficientExecutionStackException) { + throw PythonOps.RecursionError("maximum recursion depth exceeded"); + } } public static void FunctionPushFrameCodeContext(CodeContext context) { diff --git a/tests/suite/modules/system_related/test_sys.py b/tests/suite/modules/system_related/test_sys.py index 944e56c98..8b973d36f 100644 --- a/tests/suite/modules/system_related/test_sys.py +++ b/tests/suite/modules/system_related/test_sys.py @@ -742,6 +742,27 @@ def run(): t.Start() t.Join() print(out[0]) +""" + out = subprocess.check_output([sys.executable, '-c', code]) + self.assertEqual(out.strip(), b'True') + + @unittest.skipUnless(is_cli, 'IronPython specific test') + def test_setrecursionlimit_small_stack(self): + import subprocess + code = """import sys +from System.Threading import Thread, ThreadStart +sys.setrecursionlimit(10000000) +def f(n): + return f(n + 1) + 1 +def run(): + try: + f(0) + print(False) + except RecursionError: + print(True) +t = Thread(ThreadStart(run), 512 * 1024) +t.Start() +t.Join() """ out = subprocess.check_output([sys.executable, '-c', code]) self.assertEqual(out.strip(), b'True')