Bug #18903


Stack overflow signal handling seems to be triggered once and then not working after

Added by chrisseaton (Chris Seaton) about 2 years ago. Updated 9 months ago.

Target version:
ruby -v:
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]


This program creates a recursive object graph and then tries to convert it to JSON with no max depth, so it stack overflows in C code that does no co-operative stack overflow checks, as the bytecode interpreter would. This therefore triggers a segmentation fault and the stack overflow detection there. It works the first time, but the second time around it doesn't and the program hard crashes on M1.

Is there something like a guard page permission that is switched during the handling, and needs to switched back for the guard page to work again?

Note that it isn't JSON specific - I think any stack overflow within C code would do it.

require 'json'

a = []
a << a

rescue Exception
  puts 'rescued'


Updated by chrisseaton (Chris Seaton) about 2 years ago

(Found by Jean Boussier)

Updated by Eregon (Benoit Daloze) about 2 years ago

In general it is not possible to recover from a stack overflow (be it in C or Ruby), the executing program should be considered hopelessly corrupted because e.g. it might have happened in the middle of a critical section.
So IMHO the right fix would be to make stack overflows not rescue-able and hard exit on such a case.
That's from my experience with stack overflows on the JVM, some details are different on CRuby but the overall problem is the same.

Updated by mame (Yusuke Endoh) 9 months ago

Discussed at the dev meeting. @nobu (Nobuyoshi Nakada) said he would investigate if he could fix it with M2.

Recovering from a stack overflow in C is not portable and complete, but it is a fact that they work almost well in Linux, which is the main production environment. If we make it a fatal error that cannot be rescued normally, it might impact on users of Rails apps, job queues like Sidekiq, etc.


Also available in: Atom PDF