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/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 c1a44e820..8b973d36f 100644 --- a/tests/suite/modules/system_related/test_sys.py +++ b/tests/suite/modules/system_related/test_sys.py @@ -702,6 +702,67 @@ 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') + + @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')