前田です。
mod_rubyでWeakRefを使った時にWeakRef::RefErrorが発生すべきところ
でRangeErrorが発生するという報告がありました。
どうも、loadの第二引数がtrueの状態でWeakRefを使うと、オブジェクト
が回収されているのに、WeakRef::ID_MAPにIDが残ってしまうようです。
・テストスクリプト
----
require "weakref"
require "tempfile"
class Pool
@@weak_ref = nil
def Pool.get_object
begin
return @@weak_ref.__getobj__
rescue WeakRef::RefError, NameError
obj = Object.new
@@weak_ref = WeakRef.new(obj)
return obj
end
end
end
file = Tempfile.new("foo")
begin
file.puts("p Pool.get_object")
file.close
load(file.path, true)
GC.start
load(file.path, true)
ensure
file.close(true)
end
----
・実行結果
shugo@studly:~> ruby ruby/test.rb
#<Object:0x402cc9d4>
/usr/lib/ruby/1.6/weakref.rb:57:in `_id2ref': 0x201664ea is recycled object (RangeError)
from /usr/lib/ruby/1.6/weakref.rb:57:in `__getobj__'
from ruby/test.rb:9:in `get_object'
from /tmp/foo763.0:1
from ruby/test.rb:24:in `load'
from ruby/test.rb:24
--
前田 修吾