Rails 查看 ivars 在 ruby 1.9 中自燃
我有一个 Rails 3 / ruby 1.9.2 网络应用程序。每隔一段时间 - 而且可能只有 100000 个请求中的 1 个 - 我会收到一个无法解释的错误报告。
确切的错误各不相同,但在我看来,它们似乎与实例变量突然变为零有关。最明显的例子是最近发生的,助手中的这段代码
@swf_object_count||=0
@swf_object_count+=1
引发了“NoMethodError: undefined method ‘+’ for nil:NilClass”。但是,请注意,错误并不限于这种情况,这两行代码仅用于说明,而不是可以解决“解决”问题的方法。
该错误基本上不可能重现:我自己从未见过它,只看到了由此产生的错误报告。我相信当我们从 REE 切换到 ruby 1.9.2 时,该错误首先出现。
其他可能相关或不相关的细节:
- 我们在 Solaris 10 上运行 ruby1.9.2p290,使用 unicorn 分叉实例
- 我们没有使用线程或纤程(更正:我们的应用程序本身没有,但我们确实使用 NewRelic,它有一个用于收集/发布统计数据的后台线程。)
- 我们混合了 .haml 和 .haml 。 .erb 视图,但我只在 .haml 中看到过这种情况。 (不过,我们没有很多 .erb)
- 我从来没有在控制器代码中看到过这种情况,
- 我偶尔会看到有关“Bar 的未定义方法'foomethod'”的错误,当我知道我们从不调用 Bar 的事实时.foo方法。这可能与上述错误有关,其中 Bar 对象自发地替换了 Foo ivar。
我很难追踪到这一点。有什么建议,或者有人见过听起来类似的东西吗?
I have a Rails 3 / ruby 1.9.2 webapp. Every so often - and it might only be 1 request in 100000 - I get an error report that I can't explain.
The exact error varies, but they seem to be along the lines of having an instance variable in my view suddenly become nil. The clearest instance of this occurred recently, where this code in a helper -
@swf_object_count||=0
@swf_object_count+=1
- raised "NoMethodError: undefined method `+' for nil:NilClass". However, note that the errors are not limited to this case, and these two lines of code are meant only for illustration, not as something that can be worked around to 'solve' the problem.
The error is basically impossible to reproduce : I have never seen it myself, only seen the error reports resulting from it. I believe the error first appeared when we switched from REE to ruby 1.9.2.
Miscellaneous details that may or may not be relevant:
- We're running ruby1.9.2p290 on Solaris 10, using unicorn forked instances
- We're not using threads or fibers (correction: our app itself doesn't, but we do use NewRelic, which has a background thread for collecting/posting stats.)
- We have a mix of .haml & .erb views, but I've only ever seen this occur from .haml. (We don't have many .erbs, though)
- I've never seen this occur in controller code
- I've occasionally seen errors regarding "undefined method 'foomethod' for Bar", when I know for a fact that we never call Bar.foomethod. It might be possible that this is related to the bug described above, where a Bar object spontaneously replaced a Foo ivar.
I'm pretty stumped on tracking this down. Any suggestions, or has anyone seen anything that sounds similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的第一个想法是线或纤维——但你说你不使用它们。嗯...这可能是由陈旧的会话触发的吗?
这确实是一个非常广泛且定义松散的问题。
也许您可以编写一个脚本来分析您的日志文件,并尝试缩小发生这种情况的时间/路径。但这也很难调试,因为您可能在此过程中进行了一些代码更改,其中一些更改产生了看起来相似的有效错误。
Unicorn 实例可能会死亡,然后重新启动。这可能会扰乱当前会话。
这可能是您可能想要尝试的东西 - 例如,使用具有两个独角兽的测试环境,在中间会话中杀死一个,然后看看这如何影响您的日志。
您可能应该安装一个工具,每当触发异常时,您的 Rails 服务器都会通过电子邮件向您发送详细的日志记录.. 这可能是更好地了解那里发生的情况的最佳方法..
例如:您的 Gemfile 中的此 Gem:
以及您的配置中/environments/production.rb 文件:
另请参阅:
https://github.com/railsware/exception_notification
my first thought was threads or fibers -- but you said you don't use them. hmm... could this be triggered from a stale session?
this is really a very broad and loosely defined question..
Perhaps you could write a script to analyze your log files, and try to narrow down when / in which routes this happens. But that's pretty hard to debug also, because you probably made a couple of code changes along the way, some of which have produced valid errors which may look similar.
It can happen that Unicorn instances die and then they get restarted.. this could upset the current session.
This might be something you may want to experiment with -- e.g. use a test-environment with two unicorns, kill one mid-session and see how that affects your logs.
You should probably install a facility where your Rails server emails you a detailed log record whenever an exception is triggered.. that's probably the best way to get more visibility into what's happening there..
e.g.: this Gem in your Gemfile:
and in your config/environments/production.rb file:
See also:
https://github.com/railsware/exception_notification
在助手中操作视图 ivars 是一个糟糕的想法。我不确定为什么您会遇到并发问题,但我怀疑如果您以不同的方式构建代码,这些问题就会消失。
也许您可以让助手计算增量值,并在您看来执行类似
@swf_object_count + swf_increment_helper_value
的操作Manipulating view ivars in your helpers is a terrible idea. I'm not sure why you're getting the concurrency issues you're getting, but I suspect they will go away if you structure your code differently.
Perhaps you can just have the helper calculate the increment values, and in your view do something like
@swf_object_count + swf_increment_helper_value