Repository navigation
Deep recursion raises a rescuable SystemStackError instead of ending the process - #150
Conversation
RubyOps.RequireSufficientStack raises SystemStackError when the stack runs low. StackLimit asks the OS for the thread's stack bounds and keeps a 256 KB margin (a quarter of a smaller stack), because the runtime's own 64 KB check leaves too little for the exception's way out. On its own this does not save a deep interpreted recursion: the DLR interpreter rethrows from inside its catch, and on CoreCLR the rethrows nest (the next commit fixes that). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es below it Continues 4225bdf5 (#128). The stack check fired, but the SystemStackError then ended the process with a CLR stack overflow on its way out, or raised again in the rescue. On CoreCLR a catch handler runs on top of the frames the exception came out of, popped only when the catch is left; so - Build/dlr-patches/0105: the DLR interpreter (Run, for a frame with only a finally to run) and TryStatementBuilder's filter/fault lowering rethrew from inside the catch, and the rethrows nested - stack in proportion to the depth. They leave the catch and rethrow from there (ExceptionDispatchInfo). - Body.cs: a Ruby rescue clause, matching and body, ran inside the catch, with no more stack than the raise had; it runs after the handler now, still inside the try that restores $!. `begin ... rescue ... end` without rescue clauses is as it was. - Proc.BuildCall and a block's nested-return filter caught every exception to find a LambdaUnwinder; they catch LambdaUnwinder only. With the rethrow above each catch cost a capture of the growing trace: a define_method recursion took 37 s to unwind, now 1 s. - JIT-compiled bodies carry the generic body's encoded frame name, so a frame of one is "file:line:in 'Object#f'" in a backtrace, not ":0:in 'jit$f'". Exception_StackLevelTooDeep1: method, interpreted method, define_method, lambda, ensure, thread; the rescue calls methods; CRuby 4.0's output. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A thread-static read in JIT-compiled code made fib(30) 2.3x slower. An address inside the main thread's stack window has room without it, and JitRuntime.CheckStack writes that test out because the JIT stops inlining one call deeper. fib(30): master 0.0225 s, before 0.052 s, now 0.029 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The interpreter now rethrows through an ExceptionDispatchInfo, and the trace then has the catching Interpreter.Run frame twice, around the Throw frame. Matched with the interpreted frames as two, it took the caller's, and Kernel#public_send - whose TypeError comes out of an interpreted rule - came out below its caller in the backtrace (core/kernel/public_send_spec). InterpretedFrame.GroupStackFrames (patch 0105) makes them one frame again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
A JIT body enters Ruby code that makes no scope only by calling itself, so a frame needs the check once, before its first self-call: fib's leaves make none. CheckStack is inlined whole (it was measured on a Debug build, where nothing is inlined into a DynamicMethod). Release, fib(32) best of 10: master 0.0297 s, check at entry 0.035 s, now 0.0325 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fixes #128. A recursion deeper than the stack now raises a
SystemStackErrorthat the program can rescue and continue from, as in CRuby. Before, the process died of a CLR stack overflow. This holds cold (interpreted) and warm (JIT), and for methods,define_method, lambdas, blocks,method_missing,ensureand threads.What it took
RubyOps.RequireSufficientStackruns when a method or block scope is created, andJitRuntime.CheckStackruns at JIT method entry.StackLimitasks the OS for the thread's stack bounds and keeps a margin of 256 KB, or a quarter of the stack if that is less. The runtime's ownTryEnsureSufficientExecutionStackkeeps only 64 KB, too little for the filters that run while the error goes out.Run, and the filter/fault lowering inTryStatementBuilder, rethrew from inside the catch, so getting the error out needed stack in proportion to the depth. Both now capture the exception withExceptionDispatchInfo, leave the catch, and throw it from there.InterpretedFrame.GroupStackFramescollapses the catching frame that then appears twice, so backtraces are unchanged (core/kernel/public_send_speccaught this).rescuebodies run after the handler, not inside it (Body.cs). Otherwise the rescue ran on the exhausted stack and its first call raised again.LambdaUnwinder, notException(Proc.cs,BlockDefinition.cs). Every lambda frame used to catch and rethrow, and with the change above that made a deepdefine_methodunwind quadratic (37 s); it now takes about 1 s.in 'Object#jit'instead of:0:in 'jit$jit'.Cost of the check (Linux, net10.0, Release, pinned to 2 cores, best runs, seconds)
each)A JIT method checks the stack only before it calls itself, not at entry. A JIT body enters Ruby code without a scope only through a self-call, so a frame needs one check, before its first self-call, and fib's leaves make none (
JitCompiler.StackCheckPlacer). In Release,CheckStackis inlined whole. On the main thread it compares the stack pointer with the main stack's bounds, four instructions; other threads read a thread-static.fib, whose body is barely more than a self-call, is the worst case: about 10%. Without the check the branch runs at master's speed, so the check is the whole difference. Variants measured on fib(35) that were no better:
[SkipLocalsInit]: the DynamicMethod still zeroes the probe slotTesting
Exception_StackLevelTooDeep1covers five recursion shapes plus a thread. Its expected output is CRuby 4.0's.Util/parallel-sweep.sh: identical to master (core 44, library 0, language 1, command_line 0, security 2 failing).Not in this PR
Thread.new; block recursion 629 vs 1,151. On Linux IronRuby reaches 22,729 (CRuby 10,916). Getting more depth on Windows needs a bigger stack: a larger reserve for the main thread, or running the script on a thread with a largermaxStackSize.SystemStackErrorbacktrace is much longer than CRuby's (about 168k frames against 10,081), and building it takes about 0.5 s.🤖 Generated with Claude Code