Backport #2781

crash when gc_mark()ing already free'd locals of cloned scope

Added by coderrr (coderrr .) over 9 years ago. Updated 4 months ago.



This causes a segfault on >= 1.8.7-p248

def def_x(arg)
Object.send :define_method, :x do
def_x lambda{}

GC.stress = true # unnecessary but makes it occur faster
def_x nil
n = 3 # minimum for crash, increase if needed
n.times { x 0 }

This bug was caused by the fix i suggested for #1322,

The previous fix is flawed in that it added the SCOPE_MALLOC flag to the scope just so scope_dup() didn't process it. This had the side-effect that gc_mark_children() now processes the scope whereas it would not have before. A better fix is the following, which instead of adding the SCOPE_MALLOC flag, we add a check for the SCOPE_CLONE flag to scope_dup(). This fixes bug #1322 as well as the segfault:

Please check the patch for other unforseen side effects. I didn't see any changes in rubyspec failures from p174 to a patched p248.



Updated by coderrr (coderrr .) over 9 years ago

just realized the check for SCOPE_CLONE is also no longer needed before freeing locals:


Updated by coderrr (coderrr .) over 9 years ago

By the way, this causes the popular web framework sinatra to segfault due to


Updated by tmm1 (Aman Gupta) about 9 years ago

I can confirm that this is still an issue in 1.8.7-p302 (I had to increase n=3000 to reproduce on linux).

It is also causing segfaults when using Sinatra <= 0.9.5. The segfaults in Sinatra are fixed as of >= 0.9.6 with this patch:


Updated by jeremyevans0 (Jeremy Evans) 4 months ago

  • ruby -v deleted (>= 1.8.7-p248)
  • Status changed from Open to Closed
  • Description updated (diff)
  • Project changed from Ruby 1.8 to Backport187
  • Tracker changed from Bug to Backport

Also available in: Atom PDF