socket.gethostbyname unable to find local hostname

2019-09-06 03:30发布

问题:

After updating to OS Sierra, I tried running my local test suite with rspec spec, this immediately broke giving this stack trace:

Coverage report generated for RSpec to /Users/October/Documents/code/clients-api/coverage. 38 / 101 LOC (37.62%) covered.
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler/runtime.rb:94:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'torid'. (Bundler::GemRequireError)
Gem Load Error is: getaddrinfo: nodename nor servname provided, or not known
Backtrace for gem load error is:
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/torid-1.2.4/lib/torid/generator.rb:73:in `gethostbyname'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/torid-1.2.4/lib/torid/generator.rb:73:in `create_node_id'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/torid-1.2.4/lib/torid/generator.rb:79:in `<class:Generator>'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/torid-1.2.4/lib/torid/generator.rb:21:in `<module:Torid>'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/torid-1.2.4/lib/torid/generator.rb:8:in `<top (required)>'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/torid-1.2.4/lib/torid.rb:22:in `<top (required)>'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler/runtime.rb:91:in `require'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler/runtime.rb:86:in `each'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler/runtime.rb:86:in `block in require'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler/runtime.rb:75:in `each'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler/runtime.rb:75:in `require'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/bundler-1.13.2/lib/bundler.rb:106:in `require'
/Users/October/Documents/code/clients-api/config/application.rb:16:in `<top (required)>'
/Users/October/Documents/code/clients-api/config/environment.rb:2:in `require'
/Users/October/Documents/code/clients-api/config/environment.rb:2:in `<top (required)>'
/Users/October/Documents/code/clients-api/spec/spec_helper.rb:6:in `require'
/Users/October/Documents/code/clients-api/spec/spec_helper.rb:6:in `<top (required)>'
/Users/October/Documents/code/clients-api/spec/consumers/postback_notifier_consumer_spec.rb:1:in `require_relative'
/Users/October/Documents/code/clients-api/spec/consumers/postback_notifier_consumer_spec.rb:1:in `<top (required)>'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/bin/rspec:23:in `load'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/bin/rspec:23:in `<main>'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/bin/ruby_executable_hooks:15:in `eval'
/Users/October/.rvm/gems/ruby-2.3.1@clients-api/bin/ruby_executable_hooks:15:in `<main>'

I further investigated inside the torid gem to find the block of code that is failing:

def self.create_node_id( pid = Process.pid )
    hostname = Socket.gethostbyname( Socket.gethostname ).first
    random   = SecureRandom.hex( 16 )
    FNV.new.fnv1a_64("#{hostname}-#{pid}-#{random}")
 end

When I inspected it with byebug:

(byebug)hostname = Socket.gethostbyname( Socket.gethostname ).first

=> (byebug)SocketError Exception: getaddrinfo: nodename nor servname provided, or not known nil

Also gethostname does return the correct hostname:

(byebug)Socket.gethostname

=> (byebug) "OctobersMac"

At this point, I have tried re-installing homebrew, postgres, xcode, rvm and ruby. I also made sure that my hostname was set to OctobersMac. It gets stranger in the fact that when I check my ports for that hostname, none are open. I set it with scutil --get HostName and it returns correctly. I have no firewalls set up either.

I opened two other apps and ran rspec spec just to be sure that it wasn't just one app. The first came back with the exact same error and the second got a different error, though I have a feeling it has something to do with the original issue:

OctobersMac:community October$ rspec spec
"Railtie has run for Postgis!!!!"
/Users/October/.rvm/gems/ruby-2.3.1@community/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
    from /Users/October/.rvm/gems/ruby-2.3.1@ring-community/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
    from /Users/October/.rvm/gems/ruby-2.3.1@ring-community/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
    from /Users/October/.rvm/gems/ruby-2.3.1@ring-community/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
    from /Users/October/.rvm/gems/ruby-2.3.1@ring-community/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis_adapter.rb:51:in `initialize'
    from /Users/October/.rvm/gems/ruby-2.3.1@ring-community/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis/create_connection.rb:38:in `new'
    from /Users/October/.rvm/gems/ruby-2.3.1@ring-community/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis/create_connection.rb:38:in `postgis_connection'
    from /Users/October/.rvm/gems/ruby-2.3.1@ring-community/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'

Does anyone have any idea what is going on here? I'm at the end of my rope and Google doesn't have much on related issues to Mac OsX Sierra. Thank you for any time you can spare towards my issue.

回答1:

It appears that this is a problem with Sierra. The torid gem authors have a fix. In the comment they state:

MacOS Sierra does not always find the Addrinfo value for the hostname which can cause an error to happen and torid fail to work.

I came across a similar issue with the logstash gem, which has also been fixed. In their code they have a comment (in the default_source method) that states:

# NOTE: MacOS Sierra and later are setting `.local`
# hostnames that even as real hostnames without the `.local` part,
# are still unresolvable. One reliable way to get an IP is to
# get all available IP address lists and use the first one.
# This will always be `127.0.0.1`.