title: Puma Error: Cannot assign requested address - bind(2) for "10.0.2.2" port 3000 (Errno::EADDRNOTAVAIL)
body: <p>I change webrick to puma in my development environment, but I can't run rails server, look my error:</p>
  
  <pre><code>➜ r4u rails s
  =&gt; Booting Puma
  =&gt; Rails 5.0.0.1 application starting in development on http://localhost:3000
  =&gt; 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 `&lt;top (required)&gt;'
    from /home/luiz/Dropbox/projetos/r4u/bin/rails:9:in `require'
    from /home/luiz/Dropbox/projetos/r4u/bin/rails:9:in `&lt;top (required)&gt;'
    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 `&lt;top (required)&gt;'
    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 `&lt;top (required)&gt;'
    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 `&lt;top (required)&gt;'
    from bin/rails:3:in `load'
    from bin/rails:3:in `&lt;main&gt;
  </code></pre>
  
  <p>Ok, I know that <a href="https://github.com/puma/puma/issues/403" rel="nofollow">problem is in my environment</a> and that exist another proccess using this addresss. But how I can solve this? Look my netstats</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And my SS log</p>
  
  <pre><code>➜ 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                                                                                   ::1:38188                                                                                ::1:ipp 
  </code></pre>
  
  <p><em>IMPORTANT</em> I investigate the problem, occurs in <code>puma-3.6.0/lib/puma/binder.rb @ line 260 Puma::Binder#add_tcp_listener. exactly  in *TCPServer.new(host, port)*</code></p>
  
  <pre><code>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 &lt;&lt; s
    s
  end
  </code></pre>
  
  <ul>
  <li>So, <code>localhost_addresses</code> contains 2 values <code>["127.0.0.1", "10.0.2.2"]</code></li>
  <li>The error occurs when "10.0.2.2" is passed as parameter. Why 2 address? </li>
  </ul>
  
  <p>The <code>localhost_addresses</code> method</p>
  
  <pre><code>def localhost_addresses
    addrs = TCPSocket.gethostbyname "localhost"
    addrs[3..-1].uniq #- ['10.0.2.2']
  end
  </code></pre>