From: Tanaka Akira Date: 2002-04-18T13:55:29+09:00 Subject: Re: resolv.rb Bug In article <28544001472.20020417142749@milican.com>, "Roy J. Milican" writes: > Greetings, > > I wrote earlier today about a problem I was having with a scrip > that occurred after iterating through 256 emails. Well I finally > got smart and printed out the error I rescued from my resolve.rb > call. The error was "Too many open files - "/etc/resolv.conf"" The > code I was using to get the mx records was straight out of the > resolv.rb files examples in the comment. So resolv.rb is not closing > the file after it gets the dns servers. Anyone else run into the > problem? I succeed to reproduce the problem. % limit descriptors 10 % ruby -rresolv -e ' 100.times {|i| p i Resolv::DNS.new.getresources("ruby-lang.org", Resolv::DNS::Resource::IN::MX) } ' 0 1 2 3 4 5 6 7 /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:686:in `initialize': Too many open files - "/etc/resolv.conf" (Errno::EMFILE) from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:686:in `open' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:686:in `lazy_initialize' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:679:in `synchronize' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:679:in `lazy_initialize' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:356:in `lazy_initialize' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:354:in `synchronize' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:354:in `lazy_initialize' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:421:in `each_resource' from /home/akr/src/ruby/lib/ruby/1.7/resolv.rb:416:in `getresources' from -e:4 from -e:2:in `times' from -e:2 It's because each DNS object have a socket file descriptor and a thread. The file descriptor is required for communication to DNS server. The thread is required for async communication. Unfortunately the object is not GCed because it is refered from the thread. There is a workaround. Reuse a DNS object as: % ruby -rresolv -e ' dns = Resolv::DNS.new 100.times {|i| p i dns.getresources("ruby-lang.org", Resolv::DNS::Resource::IN::MX) } ' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ... -- Tanaka Akira