更新到mac osx sierra后,socket.gethostbyname无法找到本地主机名

时间:2016-10-11 00:38:28

标签: ruby-on-rails postgresql sockets homebrew macos-sierra

更新到OS Sierra之后,我尝试使用rspec spec运行我的本地测试套件,这立即打破了这个堆栈跟踪:

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>'

我在torid gem中进一步调查,以找到失败的代码块:

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

当我用byebug检查时:

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

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

gethostname确实会返回正确的主机名:

(byebug)Socket.gethostname

=&GT; (byebug) "OctobersMac"

此时,我尝试重新安装homebrewpostgresxcodervmruby。我还确保我的主机名设置为OctobersMac。当我检查我的端口中的主机名时,没有一个是开放的,这一点很奇怪。我用scutil --get HostName设置它并且它正确返回。我也没有设置防火墙。

我打开了另外两个应用并运行rspec spec,以确保它不仅仅是一个应用。第一个回到了完全相同的错误,第二个得到了不同的错误,虽然我有一种感觉它与原始问题有关:

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'

有谁知道这里发生了什么?我的绳索结束了,谷歌与Mac OsX Sierra的相关问题并不多。感谢您随时解决我的问题。

1 个答案:

答案 0 :(得分:1)

看来这是Sierra的一个问题。 torid宝石作者有一个fix。在评论中他们说:

  

MacOS Sierra并不总​​能找到主机名的Addrinfo值   这可能导致错误发生,而且无法工作。

我遇到了与logstash gem类似的问题,该问题也是fixed。在他们的代码中,他们有一个注释(在default_source方法中),声明:

# 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`.