Backport #2614
closedResolv.[getaddress] should fail gracefully on OSX when offline
Description
=begin
Hi Guys,
When OSX isn't connected to the internet, Resolv.getaddress fails abruptly due to a NoMethodError in Resolv::DNS::Config.lazy_initialize.
On OSX, /etc/resolv.conf exists only when it is connected to a network. When OSX is not (or unable) connected to any network, /etc/resolv.conf does not exist.
The cause of the NoMethodError is due to return of an uninitialized variable named "config_hash" in Resolv::DNS::Config.default_config_hash. Its only callee "lazy_initialize", expects an empty hash to be returned instead of "nil" on parsing errors (e.g. when /etc/resolv.conf does not exist).
The fix is simply to move the initialization of "config_hash" to the beginning of the function. Please see the patch attached.
Hope this helps.
Best,
Ron
NOTES¶
When OSX disconnected from internet:
irb(main):001:0> load "/opt/local/lib/ruby/1.9.1/resolv.rb"
=> true
irb(main):002:0> Resolv.getaddress "www.ruby-lang.org"
NoMethodError: undefined method include?' for nil:NilClass from /opt/local/lib/ruby/1.9.1/resolv.rb:853:in
block in lazy_initialize'
from internal:prelude:8:in synchronize' from /opt/local/lib/ruby/1.9.1/resolv.rb:832:in
lazy_initialize'
from /opt/local/lib/ruby/1.9.1/resolv.rb:332:in block in lazy_initialize' from <internal:prelude>:8:in
synchronize'
from /opt/local/lib/ruby/1.9.1/resolv.rb:330:in lazy_initialize' from /opt/local/lib/ruby/1.9.1/resolv.rb:472:in
each_resource'
from /opt/local/lib/ruby/1.9.1/resolv.rb:381:in each_address' from /opt/local/lib/ruby/1.9.1/resolv.rb:115:in
block in each_address'
from /opt/local/lib/ruby/1.9.1/resolv.rb:114:in each' from /opt/local/lib/ruby/1.9.1/resolv.rb:114:in
each_address'
from /opt/local/lib/ruby/1.9.1/resolv.rb:92:in getaddress' from /opt/local/lib/ruby/1.9.1/resolv.rb:43:in
getaddress'
from (irb):2
from /opt/local/bin/irb:12:in `'
When OSX connected to internet:
irb(main):003:0>Resolv.getaddress "www.ruby-lang.org"
=> "221.186.184.68"
=end
Files