Ruby 堆栈级别太深异常不是来自递归无限循环 [英] Ruby stack level too deep exception NOT from recursive infinite loop

查看:46
本文介绍了Ruby 堆栈级别太深异常不是来自递归无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(已解决)实际上它可能是由于无限循环而引发的

我正在编码,在添加了一个方法后,我得到了这个:

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.

代码现在以某种方式进行了优化,但我也遇到了直接运行 ruby​​ 文件的错误.

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...

  • ruby 1.9.3p194/rails 3.2.8,通过 rvm 安装
  • 此阶段的程序只使用了 rails 字符串拐点函数
  • 操作系统:linux kubuntu i386
  • 记忆4GO
  • 'ulimit -s' : 8192(以 kB 为单位的堆栈大小)
  • 删除了最初引发异常的代码块,但在运行时稍后仍会引发异常
  • 使用命令行ulimit -s 20000"、ulimit -s unlimited"设置堆栈大小.同样的错误,显然在同一个地方(这让我觉得堆栈大小实际上没有改变)
  • 降级到 ruby​​1.9.2/rails3.1.3,得到同样的消息
  • Windows 下同样的错误

我正在编写一个大量使用 ruby​​ mixin 的应用程序.

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

此外,我创建了一堆生成 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.

但是,当我编写位于这个库之上的程序时,这最终应该让我在 as$ 中省去不少痛苦(无论如何这就是计划).

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).

在某些代码可用于显示/测试之前,让我们将我的问题抽象为这个问题:除了经典的程序执行树之外,是否还有其他情况会引发堆栈级别太深的异常- 场景太深(交叉手指很清楚,意味着什么......)?

推荐答案

除了经典的 program-execution-tree-too-deep 场景之外,是否还有其他情况会导致堆栈级别过深异常?

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

在这个例子中,一个只有一个变量的函数在失败之前可以进行数千次调用,而添加三个变量的作用很小;但是再添加 26 个变量会使堆栈大小膨胀到只有大约 3000 个级别可用的程度.

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.

这当然在一定程度上取决于 ruby​​ 实现,以及它运行的系统.但我相信它会永远成为一般规则.

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.

这篇关于Ruby 堆栈级别太深异常不是来自递归无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆