From: Tanaka Akira Date: 2009-09-11T16:02:58+09:00 Subject: [ruby-dev:39309] Re: [Bug #2067] bodyが大きいエラーページをopen-uriで取得するとfdがリークしている In article , takeru sasaki writes: > 下記スクリプトを実行すると1.8(ちょっと古いのですが)では251回目で > Errno::EMFILEとなってしまいました。 > 例外オブジェクトが持つioがcloseされないようです。 縮めるとこんな感じでしょうか。 tmperr() {} を tmperr() にすると回収されるようになるのが非常 に怪しいです。 % cat z.rb require 'tempfile' class Err < StandardError def initialize(io) @io = io end end def tmperr() # :nodoc: tmpf = Tempfile.new('open-uri') raise Err.new(tmpf) end 2000.times do |n| puts "n=#{n}" begin tmperr() {} rescue Err => e end p Dir.entries("/proc/self/fd").sort_by {|n| n.to_i } numio = 0; ObjectSpace.each_object(IO) {|io| numio += 1 }; p [:numio, "*" * numio] numex = 0; ObjectSpace.each_object(Err) {|io| numex += 1 }; p [:numex, "*" * numex] GC.start end % limit descriptors 20 % ruby-1.8 -v z.rb ruby 1.8.8dev (2009-08-30 revision 24713) [i686-linux] n=0 [".", "..", "0", "1", "2", "3", "4", "5"] [:numio, "******"] [:numex, "*"] n=1 [".", "..", "0", "1", "2", "3", "4", "5", "6"] [:numio, "*****"] [:numex, "**"] n=2 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7"] [:numio, "******"] [:numex, "***"] n=3 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8"] [:numio, "*******"] [:numex, "****"] n=4 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] [:numio, "********"] [:numex, "*****"] n=5 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] [:numio, "*********"] [:numex, "******"] n=6 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] [:numio, "**********"] [:numex, "*******"] n=7 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"] [:numio, "***********"] [:numex, "********"] n=8 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"] [:numio, "************"] [:numex, "*********"] n=9 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"] [:numio, "*************"] [:numex, "**********"] n=10 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"] [:numio, "**************"] [:numex, "***********"] n=11 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"] [:numio, "***************"] [:numex, "************"] n=12 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"] [:numio, "****************"] [:numex, "*************"] n=13 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"] [:numio, "*****************"] [:numex, "**************"] n=14 [".", "..", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"] [:numio, "******************"] [:numex, "***************"] n=15 z.rb:20:in `open': Too many open files - /proc/self/fd (Errno::EMFILE) from z.rb:20:in `entries' from z.rb:20 from z.rb:14:in `times' from z.rb:14 -- [田中 哲][たなか あきら][Tanaka Akira]