[英]RuntimeError: can't add a new key into hash during iteration in Rack
[英]RuntimeError: can't add a new key into hash during iteration
我遇到很多RuntimeError: can't add a new key into hash during iteration
在Rails应用中RuntimeError: can't add a new key into hash during iteration
错误RuntimeError: can't add a new key into hash during iteration
,但是堆栈跟踪没有指向任何有趣的地方。
https://gist.github.com/tal-moshayov/71e06a83cee236fdedd0
任何提示将不胜感激
[添加锁以使共享的哈希线程安全]
我遇到了非常类似的问题,问题是一个线程试图修改共享对象(哈希),而另一个线程正在迭代它。 最终,我通过添加锁(Mutex)并使用自己的Pure-Ruby并发Hash进行了整理。
[检查所有线程回溯以查找谁拥有/迭代您的哈希]
您在尝试修改“忙”哈希时遇到的堆栈跟踪问题是,它向您显示了功能链,直到您试图修改哈希的地方,但是却没有显示谁正在并行地迭代它(谁拥有它)! 但是我找到了通过打印所有当前正在运行的线程的堆栈跟踪来解决这一问题的方法。 这是您的操作方式:
# This solution was found in comment by @thedarkone on https://github.com/rails/rails/issues/24627
rescue Object => boom
# only for your particular exception:
if boom.message == "can't add a new key into hash during iteration"
thread_count = 0
Thread.list.each do |t|
thread_count += 1
err_msg += "--- thread #{thread_count} of total #{Thread.list.size} #{t.object_id} backtrace begin \n"
# Lets see if we are able to pin down the culprit
# by collecting backtrace for all existing threads:
err_msg += t.backtrace.join("\n")
err_msg += "\n---thread #{thread_count} of total #{Thread.list.size} #{t.object_id} backtrace end \n"
end
# and just print it somewhere you like:
$stderr.puts(err_msg)
raise # always reraise
end
上面的代码段即使对于教育目的也很有用,因为它可以向您显示(例如X射线)实际有多少个线程(相对于您以为有多少个线程-通常这两个是不同的数字;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.