Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/etc/hosts can prevent puma from starting #1022

Closed
AKovtunov opened this issue Jul 20, 2016 · 15 comments
Closed

/etc/hosts can prevent puma from starting #1022

AKovtunov opened this issue Jul 20, 2016 · 15 comments
Labels

Comments

@AKovtunov
Copy link

AKovtunov commented Jul 20, 2016

After some coding got this error on running rails s:

puma-3.5.0/lib/puma/binder.rb:268:in `initialize': Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)

My environment is:

$ rails -v       
Rails 5.0.0
$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]

I've tried:

  1. Create a new project - the same
  2. Checked Rails 4.2 - problem solved
  3. Reinstall rails 5 and ruby - the same problem
  4. lsof -wni tcp:3000 returns me nothing
  5. ps aux | grep "rails" - nothing
  6. ps aux | grep "puma" - nothing
  7. ps aux | grep "ruby" -nothing
  8. Use puma instead of rails s - problem solved
  9. Use RAILS_ENV=production bundle exec rails s - problem solved
  10. Use rails s -p 3001 - same problem, for other ports too

Monkey-patched https://github.com/puma/puma/blob/master/lib/puma/binder.rb and added pry.
Found that TCPServer.new is called few times with 127.0.0.1:3000

[3] pry(#<Puma::Binder>)> TCPSocket.gethostbyname "localhost"
=> ["localhost",
 [my hosts?],
 2,
 "127.0.0.1",
 "127.0.0.1",
 "127.0.0.1",
 "127.0.0.1",
 "127.0.0.1"]

I think this commit broke something for my machine 9b1de10#diff-4fd9276d9f86d7f4827e6ca4ab6b82b8
And I think this is related to the #782

@AKovtunov
Copy link
Author

Downgrading to 3.4.0 works as charm

@evanphx
Copy link
Member

evanphx commented Jul 20, 2016

Oh geez. Ok, I’ll fix this asap and have 3.5.1 out.

On Wed, Jul 20, 2016 at 2:46 AM Alexandr Kovtunov notifications@github.com
wrote:

Downgrading to 3.4.0 works as charm


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#1022 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAAAByheOt_CQU9hu8-pmogNxalForXIks5qXe6AgaJpZM4JQmLn
.

@evanphx
Copy link
Member

evanphx commented Jul 20, 2016

@AKovtunov i'm curious, what does your /etc/hosts file look like? I'm wondering why localhost returned 127.0.0.1 multiple times..

@AKovtunov
Copy link
Author

AKovtunov commented Jul 20, 2016

After that I was wondering the same too and cleared a bit my hosts file.
I was working on a project with multi-service architecture and needed to bind some routes to 127.0.0.1 :)

But the error is still here, here is my basic hosts that were installed by ubuntu

[1] pry(#<Puma::Binder>)> localhost_addresses
=> ["127.0.0.1", "127.0.0.1"]
[2] pry(#<Puma::Binder>)> TCPSocket.gethostbyname "localhost"
=> ["localhost", ["alexandr-Lenovo-IdeaPad-Y500", "localhost", "alexandr-Lenovo-IdeaPad-Y500.localhost"], 2, "127.0.0.1", "127.0.0.1"]
‹ruby-2.3.1›  ‹master*› $ cat /etc/hosts                                                                                                              1 ↵
127.0.0.1   localhost
127.0.1.1   alexandr-Lenovo-IdeaPad-Y500
127.0.0.1 alexandr-Lenovo-IdeaPad-Y500.localhost alexandr-Lenovo-IdeaPad-Y500 localhost
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

@schuetzm
Copy link

Please reopen, the bugfix in 84fdc1b does not work if the addresses for localhost are ["127.0.0.1", "::1"].

To reproduce, put the following lines into your /etc/hosts:

127.0.0.1   localhost
::1         localhost ipv6-localhost ipv6-loopback

@daya
Copy link

daya commented Nov 29, 2016

@evanphx please reopen this issue @schuetzm steps are enough to reproduce the issue

@schuetzm
Copy link

@daya Which error are you getting exactly? "Address already in use", or "Address not available"? If it's the latter, it's likely a misconfiguration on your side, see #1062.

@nateberkopec nateberkopec changed the title Rails 5 can't be started for puma 3.5.0 /etc/hosts can prevent puma from starting Feb 18, 2017
@delner
Copy link

delner commented May 9, 2017

Getting this error in 3.6.2, while running in Rails & Docker via rails s:

/usr/local/bundle/gems/puma-3.6.2/lib/puma/binder.rb:266:in `initialize': Cannot assign requested address - bind(2) for "::1" port 3000 (Errno::EADDRNOTAVAIL)
	from /usr/local/bundle/gems/puma-3.6.2/lib/puma/binder.rb:266:in `new'
	from /usr/local/bundle/gems/puma-3.6.2/lib/puma/binder.rb:266:in `add_tcp_listener'

/etc/hosts:

127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.20.0.5	4b1ea4eb06a8

Running with rails s -b 0.0.0.0 allows it to start.

@nateberkopec
Copy link
Member

If you have both 127.0.0.0 and ::1 pointing to localhost, puma will try to bind to both and then fail. I'm not sure this is really a bug, it's doing exactly what it's supposed to do. Pointing ::1 to localhost is user error IMO, you should just not do that. LMK if there's some reason why you have to/must do this, otherwise I'm closing.

@delner
Copy link

delner commented Jun 5, 2017

@nateberkopec Just an FYI, that /etc/hosts is not a user specific configuration, thats the default from running Ubuntu in Docker. Anyone who uses that image in Docker (a ton of people) could possibly have the same problem.

@nateberkopec
Copy link
Member

running Ubuntu in Docker

Oh alright. I'll reopen until I get a repro. I'm looking at this in #1318.

@nateberkopec nateberkopec reopened this Jun 5, 2017
@delner
Copy link

delner commented Jun 5, 2017

If you'd like to reproduce the hosts file state via Docker, you can do that via docker run --rm ubuntu cat /etc/hosts. As far as Puma itself is concerned, I don't have a repro for you.

@nateberkopec
Copy link
Member

Fixed in #1318

@gcaracausi
Copy link

You have to modify /etc/hosts and remove localhost from the ipv6 line.
From ::1 localhost ip6-localhost ip6-loopback to ::1 ip6-localhost ip6-loopback

@dimisjim
Copy link

@gcaracausi @nateberkopec Indeed, puma binding works when you remove the localhost entry from the ::1 line.

Can easily be done with a dockerfile such as this:

RUN cp /etc/hosts /etc/hosts2 && sed -i 's/::1	localhost/::1/g' /etc/hosts2
CMD cat /tmp/hosts >> /etc/hosts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants