Ruby stack level too deep exception NOT from recursive infinite loop

2.8k views Asked by At

EDIT : (SOLVED) actually it probably was raised BECAUSE OF an infinite loop

I was coding and after adding a method I got this :

user_name@the_computer:/media/ECC3-C3B0/Prog/mts/src/mts$ rake test --trace
** Invoke test (first_time)
** Execute test
/home/user_name/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36: stack level too deep (SystemStackError)
rake aborted!
Command failed with status (1): [/home/user_name/.rvm/rubies/ruby-1.9.3-p19...]
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/file_utils.rb:53:in `block in create_shell_runner'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/file_utils.rb:45:in `call'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/file_utils.rb:45:in `sh'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:39:in `sh'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/file_utils.rb:82:in `ruby'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:39:in `ruby'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/testtask.rb:99:in `block (2 levels) in define'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/file_utils_ext.rb:60:in `verbose'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/testtask.rb:98:in `block in define'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/home/user_name/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `load'
/home/user_name/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `<main>'
/home/user_name/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `eval'
/home/user_name/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => test

I'm pretty sure there is no infinite recursive loop involved.

The code is for now somehow gemified, but I also got the error running the ruby file directly.

Thank you for any help on how to (get some information to, run some tests to) fix the problem, if possible without having to rewrite the whole thing...

Environment :

  • ruby 1.9.3p194 / rails 3.2.8, installed via rvm
  • the program at this stage only uses rails string inflexions functions
  • OS : linux kubuntu i386
  • memory 4GO
  • 'ulimit -s' : 8192 (stack size in kB)

What I tried unsuccessfully :

  • removed the chunk of code where the exception was initially raised, but it was still raised a tiny bit later at runtime
  • set stack size with command line 'ulimit -s 20000', 'ulimit -s unlimited'. Same error, apparently at the same place (which makes me think the stack size wasn't actually changed)
  • downgraded to ruby1.9.2 / rails3.1.3, got the same message
  • same error under Windows

Application context :

I'm writing an application that heavily uses ruby mixins.

Besides I created a bunch of classes that generate mixins (instance / class methods modules to be included by other classes).

So all in all I end up with quite a bit of generated named modules with some custom generated code, and classes with many ancestors.

But that should eventually save me quite a bit of pain in the as$ when I write the program that sits on top of this lib (that's the plan anyway).

Resources I used :

EDIT : Until some code is available for the showing/testing, let's abstract my question down to this one : Are there other cases that raise stack level too deep exception, besides the classical program-execution-tree-is-too-deep scenario (crossing fingers it's clear and means something...) ?

1

There are 1 answers

2
marcus erronius On BEST ANSWER

Are there other cases that raise stack level too deep exception, besides the classical program-execution-tree-is-too-deep scenario?

Yes. Since the stack is not measured in depth, but in bytes, anything stored on the stack will fill it up sooner:

def recurse(depth=0)
  recurse depth+1
rescue SystemStackError
  depth
end
=> nil
recurse
=> 8717

def recurse(depth=0)
  a,b,c = 1,2,3
  recurse depth+1
rescue SystemStackError
  depth
end
=> nil
recurse
=> 7264

def recurse(depth=0)
  a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z = *(0..25)
  recurse depth+1
rescue SystemStackError
  depth
end
=> nil
recurse
=> 3187

In this example, a function with only one variable can go several thousand calls deep before failing, and adding three more variables does very little; but adding 26 more variables balloons the stack size to a point where only around 3000 levels are available.

This will, of course depend somewhat on the ruby implementation, and the system it's running on. But I believe it will always hold as a general rule.

However, I still think recursion is likely your problem, since the number of variables required to have this happen at small call chain lengths is immense.