Puma Error: Cannot assign requested address

房东的猫 提交于 2020-01-06 19:43:10

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

--------------------------------------------------------------------【可能出现的问题及解决】

Seems like you have 10.0.2.2 in your localhosts.
You can check it in /etc/hosts.
It may look like

...
127.0.0.1       localhost
10.0.2.2        localhost
...

If you don't need 10.0.2.2 here and you don't know why you got it (your case =)) you can remove this line and try to run rails s again.
Maybe you would have to restart your network system or you can reboot.

Another way would be running rails server on particular host like

rails s -b 127.0.0.1

I don't know exact reasons of the problem but I hope it helps you somehow.

--------------------------------------------------------------------

-------------------------------------------------------------------【原问题】

I change webrick to puma in my development environment, but I can't run rails server, look my error:

➜ r4u rails s
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
127.0.0.1
10.0.2.2
Exiting
/home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:267:in `initialize': Cannot assign requested address - bind(2) for "10.0.2.2" port 3000 (Errno::EADDRNOTAVAIL)
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:267:in `new'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:267:in `add_tcp_listener'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:260:in `block in add_tcp_listener'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:259:in `each'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:259:in `add_tcp_listener'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:102:in `block in parse'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:85:in `each'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/binder.rb:85:in `parse'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/runner.rb:133:in `load_and_bind'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/single.rb:85:in `run'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/puma/launcher.rb:172:in `run'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/puma-3.6.0/lib/rack/handler/puma.rb:51:in `run'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/rack-2.0.1/lib/rack/server.rb:296:in `start'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/server.rb:79:in `start'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:90:in `block in server'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `server'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
  from /home/luiz/Dropbox/projetos/r4u/bin/rails:9:in `require'
  from /home/luiz/Dropbox/projetos/r4u/bin/rails:9:in `<top (required)>'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `load'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `call'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/spring-1.7.2/lib/spring/client/command.rb:7:in `call'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/spring-1.7.2/lib/spring/client.rb:30:in `run'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/spring-1.7.2/bin/spring:49:in `<top (required)>'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `load'
  from /home/luiz/.rvm/gems/ruby-2.3.1/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `<top (required)>'
  from /home/luiz/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  from /home/luiz/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  from /home/luiz/Dropbox/projetos/r4u/bin/spring:13:in `<top (required)>'
  from bin/rails:3:in `load'
  from bin/rails:3:in `<main>

Ok, I know that problem is in my environment and that exist another proccess using this addresss. But how I can solve this? Look my netstats

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:17603         0.0.0.0:*               LISTEN      2975/dropbox    
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1401/mysqld     
tcp        0      0 127.0.0.1:5939          0.0.0.0:*               LISTEN      1497/teamviewerd
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2079/dnsmasq    
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      1507/dnsmasq    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      4437/cupsd      
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1413/postgres   
tcp        0      0 0.0.0.0:17500           0.0.0.0:*               LISTEN      2975/dropbox    
tcp        0      0 127.0.0.1:17600         0.0.0.0:*               LISTEN      2975/dropbox    
tcp6       0      0 :::80                   :::*                    LISTEN      2049/apache2    
tcp6       0      0 ::1:631                 :::*                    LISTEN      4437/cupsd      
tcp6       0      0 :::17500                :::*                    LISTEN      2975/dropbox    
udp        0      0 0.0.0.0:631             0.0.0.0:*                           1315/cups-browsed
udp        0      0 0.0.0.0:17500           0.0.0.0:*                           2975/dropbox    
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           3282/chrome     
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           3282/chrome     
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           1142/avahi-daemon: 
udp        0      0 0.0.0.0:14381           0.0.0.0:*                           1464/dhclient   
udp        0      0 0.0.0.0:52083           0.0.0.0:*                           1142/avahi-daemon: 
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2079/dnsmasq    
udp        0      0 127.0.1.1:53            0.0.0.0:*                           1507/dnsmasq    
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2079/dnsmasq    
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1464/dhclient   
udp6       0      0 :::5353                 :::*                                3282/chrome     
udp6       0      0 :::5353                 :::*                                1142/avahi-daemon: 
udp6       0      0 :::42534                :::*                                1142/avahi-daemon: 
udp6       0      0 :::22297                :::*                                1464/dhclient   
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     16311    2521/gnome-keyring- /run/user/1000/keyring-44aQ70/control
unix  2      [ ACC ]     STREAM     LISTENING     10981    2139/Xorg           /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     21805    3282/chrome         /tmp/.com.google.Chrome.ij7UG8/SingletonSocket
unix  2      [ ACC ]     STREAM     LISTENING     160803   18684/spring server /run/user/1000/spring-1000/2ce6127e98884ac8253b4dfd188b6d2e
unix  2      [ ACC ]     STREAM     LISTENING     17883    2702/gnome-session  /tmp/.ICE-unix/2702
unix  2      [ ACC ]     STREAM     LISTENING     16689    2521/gnome-keyring- /run/user/1000/keyring-44aQ70/pkcs11
unix  2      [ ACC ]     STREAM     LISTENING     16693    2521/gnome-keyring- /run/user/1000/keyring-44aQ70/ssh
unix  2      [ ACC ]     STREAM     LISTENING     16694    2521/gnome-keyring- /run/user/1000/keyring-44aQ70/gpg
unix  2      [ ACC ]     STREAM     LISTENING     17882    2702/gnome-session  @/tmp/.ICE-unix/2702
unix  2      [ ACC ]     STREAM     LISTENING     18531    2780/pulseaudio     /run/user/1000/pulse/native
unix  2      [ ACC ]     STREAM     LISTENING     10980    2139/Xorg           @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     30610    3029/gvfsd-trash    @/dbus-vfs-daemon/socket-q8PPBuMR
unix  2      [ ACC ]     STREAM     LISTENING     7237     1/init              @/com/ubuntu/upstart
unix  2      [ ACC ]     SEQPACKET  LISTENING     9210     404/systemd-udevd   /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     10185    1014/dbus-daemon    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     14343    1103/bluetoothd     /var/run/sdp
unix  2      [ ACC ]     STREAM     LISTENING     14415    1142/avahi-daemon:  /var/run/avahi-daemon/socket
unix  2      [ ACC ]     STREAM     LISTENING     15262    2708/dbus-daemon    @/tmp/dbus-enRuhGcuXb
unix  2      [ ACC ]     STREAM     LISTENING     11808    1322/acpid          /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     14534    1413/postgres       /var/run/postgresql/.s.PGSQL.5432
unix  2      [ ACC ]     STREAM     LISTENING     17734    2523/init           @/com/ubuntu/upstart-session/1000/2523
unix  2      [ ACC ]     STREAM     LISTENING     14064    1372/libvirtd       /var/run/libvirt/libvirt-sock
unix  2      [ ACC ]     STREAM     LISTENING     14065    1372/libvirtd       /var/run/libvirt/libvirt-sock-ro
unix  2      [ ACC ]     STREAM     LISTENING     14584    1401/mysqld         /var/run/mysqld/mysqld.sock
unix  2      [ ACC ]     STREAM     LISTENING     24298    3818/gvfsd-http     @/dbus-vfs-daemon/socket-vY0yhUY7
unix  2      [ ACC ]     STREAM     LISTENING     17751    2642/dbus-daemon    @/tmp/dbus-QUMescAA1H
unix  2      [ ACC ]     STREAM     LISTENING     17335    2975/dropbox        /home/luiz/.dropbox/command_socket
unix  2      [ ACC ]     STREAM     LISTENING     19116    2975/dropbox        /home/luiz/.dropbox/iface_socket
unix  2      [ ACC ]     STREAM     LISTENING     17829    2666/ibus-daemon    @/tmp/dbus-trIHWO34
unix  2      [ ACC ]     STREAM     LISTENING     27107    4437/cupsd          /var/run/cups/cups.sock

And my SS log

➜ r4u ss -tp 
State       Recv-Q Send-Q                                                                    Local Address:Port                                                                        Peer Address:Port   
ESTAB       0      0                                                                            10.0.0.107:49782                                                                      187.73.143.66:http     users:(("chrome",3282,226))
CLOSE-WAIT  1      0                                                                            10.0.0.107:35032                                                                       209.20.75.76:http     users:(("plugin_host",4558,114))
ESTAB       0      0                                                                            10.0.0.107:38126                                                                        52.73.27.41:https    users:(("dropbox",2975,119))
ESTAB       0      0                                                                            10.0.0.107:51506                                                                     64.233.190.189:https    users:(("chrome",3282,95))
ESTAB       0      0                                                                            10.0.0.107:53868                                                                     198.252.206.25:https    users:(("chrome",3282,283))
CLOSE-WAIT  1      0                                                                            10.0.0.107:45888                                                                       91.189.94.25:http     users:(("ubuntu-geoip-pr",3738,9))
ESTAB       0      0                                                                            10.0.0.107:58574                                                                     192.30.253.124:https    users:(("chrome",3282,273))
LAST-ACK    1      1                                                                            10.0.0.107:58274                                                                       52.7.106.188:https   
ESTAB       0      0                                                                            10.0.0.107:53918                                                                     198.252.206.25:https    users:(("chrome",3282,308))
ESTAB       0      0                                                                            10.0.0.107:38220                                                                     151.101.193.69:http     users:(("chrome",3282,310))
CLOSE-WAIT  32     0                                                                            10.0.0.107:33284                                                                         45.58.74.1:https    users:(("dropbox",2975,148))
ESTAB       0      0                                                                            10.0.0.107:54614                                                                      216.58.202.46:https    users:(("chrome",3282,94))
ESTAB       0      0                                                                            10.0.0.107:38194                                                                     151.101.193.69:http     users:(("chrome",3282,306))
ESTAB       0      0                                                                            10.0.0.107:53882                                                                      216.58.202.46:https    users:(("chrome",3282,113))
ESTAB       0      0                                                                            10.0.0.107:49496                                                                      52.84.170.161:https    users:(("dropbox",2975,130))
ESTAB       0      0                                                                            10.0.0.107:33866                                                                     151.101.129.69:http     users:(("chrome",3282,296))
CLOSE-WAIT  32     0                                                                            10.0.0.107:52664                                                                    108.160.172.204:https    users:(("dropbox",2975,34))
ESTAB       0      0                                                                            10.0.0.107:33934                                                                     151.101.129.69:http     users:(("chrome",3282,231))
ESTAB       0      0                                                                            10.0.0.107:57396                                                                    173.255.112.173:https    users:(("chrome",3282,206))
CLOSE-WAIT  32     0                                                                            10.0.0.107:35746                                                                    108.160.172.225:https    users:(("dropbox",2975,99))
ESTAB       0      0                                                                            10.0.0.107:57878                                                                      201.67.45.154:http     users:(("chrome",3282,158))
ESTAB       0      0                                                                            10.0.0.107:51750                                                                     162.125.34.129:https    users:(("dropbox",2975,110))
ESTAB       0      0                                                                            10.0.0.107:38246                                                                     64.233.190.188:5228     users:(("chrome",3282,115))
ESTAB       0      0                                                                            10.0.0.107:55408                                                                      216.58.202.46:https    users:(("chrome",3282,128))
ESTAB       0      0                                                                            10.0.0.107:58602                                                                     192.30.253.124:https    users:(("chrome",3282,295))
ESTAB       0      0                                                                            10.0.0.107:44630                                                                      216.58.202.37:https    users:(("chrome",3282,257))
CLOSE-WAIT  1      0       

IMPORTANT I investigate the problem, occurs in puma-3.6.0/lib/puma/binder.rb @ line 260 Puma::Binder#add_tcp_listener. exactly in *TCPServer.new(host, port)*

def add_tcp_listener(host, port, optimize_for_latency=true, backlog=1024)
  if host == "localhost"
    localhost_addresses.each do |addr|
      add_tcp_listener addr, port, optimize_for_latency, backlog
    end
    return
  end

  host = host[1..-2] if host and host[0..0] == '['
  s = TCPServer.new(host, port)
  if optimize_for_latency
    s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
  end
  s.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
  s.listen backlog
  @connected_port = s.addr[1]

  @ios << s
  s
end
  • So, localhost_addresses contains 2 values ["127.0.0.1", "10.0.2.2"]
  • The error occurs when "10.0.2.2" is passed as parameter. Why 2 address?

The localhost_addresses method

def localhost_addresses
  addrs = TCPSocket.gethostbyname "localhost"
  addrs[3..-1].uniq #- ['10.0.2.2']
end

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!