3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】portを使っていないのにAddress already in useエラーが出る場合の解決法

Last updated at Posted at 2020-02-23

はじめに

少し昔のrailsアプリ少しいじろうかと思ったのですが、見事にハマりました。
bundle updateだとかgem pristineだとかをしたのですが、結局rails serverができなかったので、奮闘記録と共にとりあえずのサーバー起動方法を書いておきます。

(3/11追記)コメントによって解決しました。

動作環境

  • Ruby 2.5.3
  • Rails 6.0.2.1, 5.2.4.1
  • puma 3.12.2
  • ローカル環境

Address already in useエラー

$ rails s
=> Booting Puma
=> Rails 6.0.2.1 application starting in development 
=> Run `rails server --help` for more startup options
[1234] Puma starting in cluster mode...
[1234] * Version 4.3.1 (ruby 2.5.3-p105), codename: Mysterious Traveller
[1234] * Min threads: 5, max threads: 5
[1234] * Environment: development
[1234] * Process workers: 2
[1234] * Preloading application
[1234] * Listening on tcp://127.0.0.1:3000
[1234] * Listening on tcp://[::1]:3000
Exiting
Traceback (most recent call last):
        37: from bin/rails:3:in `<main>'
        36: from bin/rails:3:in `load'
        35: from /Users/k_end/workspace/personal/kiite_app/bin/spring:15:in `<top (required)>'
        34: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
        33: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
        32: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
        31: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
        30: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
        29: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
        28: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
        27: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
        26: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
        25: from /Users/k_end/workspace/personal/kiite_app/bin/rails:9:in `<top (required)>'
        24: from /Users/k_end/workspace/personal/kiite_app/bin/rails:9:in `require'
        23: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-6.0.2.1/lib/rails/commands.rb:18:in `<top (required)>'
        22: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-6.0.2.1/lib/rails/command.rb:46:in `invoke'
        21: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-6.0.2.1/lib/rails/command/base.rb:69:in `perform'
        20: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-1.0.1/lib/thor.rb:392:in `dispatch'
        19: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command'
        18: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-1.0.1/lib/thor/command.rb:27:in `run'
        17: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:138:in `perform'
        16: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:138:in `tap'
        15: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:147:in `block in perform'
        14: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-6.0.2.1/lib/rails/commands/server/server_command.rb:39:in `start'
        13: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/rack-2.2.2/lib/rack/server.rb:327:in `start'
        12: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/rack/handler/puma.rb:73:in `run'
        11: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/launcher.rb:172:in `run'
        10: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/cluster.rb:413:in `run'
         9: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/runner.rb:161:in `load_and_bind'
         8: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:90:in `parse'
         7: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:90:in `each'
         6: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:106:in `block in parse'
         5: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:222:in `add_tcp_listener'
         4: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:222:in `each'
         3: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:223:in `block in add_tcp_listener'
         2: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:229:in `add_tcp_listener'
         1: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:229:in `new'
/Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:229:in `initialize': Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)

サーバーを起動させたままコンソールを閉じてしまうというよくあるパターンだと思い、いつも通りの手順を踏む。

$ ps ax | grep rails
  834 s000  S+     0:00.00 grep --color=auto rails

834ps ax | grep railsによるプロセスなので、他に動いているプロセスはない。

$ ps aux | grep puma
k_end              882   0.0  0.0  4276968    712 s000  R+    7:50AM   0:00.00 grep --color=auto puma

こちらも同様。

$ lsof -wni tcp:3000

もちろん意味なし。
この辺りで、いやまさかそんなはずが……と思い始める。

ポートを使っているか確認してみた

teratailのRails sでポート3000番で立ち上がらないのコメントを参考にし、bashで確認したところ、

$ exec 3<>/dev/tcp/localhost/3000
bash: connect: Connection refused
bash: /dev/tcp/localhost/3000: Connection refused

ポート使っていないですね。
ということは、他のアプリなら起動できるのでは?と思い、やってみたところ……

$ rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 4.3.1 (ruby 2.6.5-p114), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop

行けるんじゃん!!
やっぱり、ポートの問題ではなく、違うところでエラーがあるらしい。

ということは、これはpumaの問題かなあ。というか、とりあえずでアップデートしたせいかrailsのバージョンも6台になってる。

奮闘の記録

pumaをシングルモードにしてみた

Puma starting in cluster mode...とあったので、うまく動く方に合わせてsingle modeにしてみる。
参考:Pumaの使い方 まとめ

$ rails s
=> Booting Puma
=> Rails 6.0.2.1 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.1 (ruby 2.5.3-p105), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000
Exiting
Traceback (most recent call last):
        37: from bin/rails:3:in `<main>'
        36: from bin/rails:3:in `load'
        35: from /Users/k_end/workspace/personal/kiite_app/bin/spring:15:in `<top (required)>'
        34: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
         .
         .
         .
         2: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:229:in `add_tcp_listener'
         1: from /Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:229:in `new'
/Users/k_end/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-4.3.1/lib/puma/binder.rb:229:in `initialize': Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)

single modeにはなったけど、やっぱりだめ。

Rails をダウングレードしてみた

gem 'rails', '~> 5.2.3'を指定し、Gemfile.lockを削除してbundle install

結果、変わらず。

とりあえずの応急処置

pumaコマンドで起動

pumaコマンドでconfig/puma.rbを指定して起動する。

$ bundle exec puma -t 5:5 -p 3000 -e development -C config/puma.rb
Puma starting in single mode...
* Version 3.12.2 (ruby 2.5.3-p105), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

pumaには問題はないのか?

WEBrickで起動

エラーが出ていたファイルが~/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/puma-3.12.2/lib/puma/binder.rbだったので、とりあえずpumaディレクトリごと削除。

$ rails s
=> Rails 5.2.4.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[2020-02-23 10:06:46] INFO  WEBrick 1.4.2
[2020-02-23 10:06:46] INFO  ruby 2.5.3 (2018-10-18) [x86_64-darwin17]
[2020-02-23 10:06:46] INFO  WEBrick::HTTPServer#start: pid=21009 port=3000

WEBrickではちゃんとサーバーを起動できている。ということは、やっぱりpumaが原因だ。

ポート番号3000を指定して起動

$ rails s -p 3000
=> Booting Puma
=> Rails 5.2.4.1 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.2 (ruby 2.5.3-p105), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

問題なく起動できます。

以上の3通りの方法で起動することができました。一番普通と同じ動きをするのは、この方法かと思われます。

おわりに

この通り、とりあえずサーバーを起動する方法はありますが、いまだにrails sコマンドでは起動できていません。
rails srails s -port 3000ではポート番号を設定する部分が違うので、多分そこがぶっ壊れてるんだろうなと思っています。
解決策などございましたら、ぜひコメントをいただければ幸いです。

(追記)解決法

以下の部分でポートの設定が重複していたので、下の記述を削除したところ正常に起動させることができました。

config/puma.rb
port        ENV.fetch("PORT") { 3000 }
port        ENV['PORT']     || 3000

以前、herokuにアップロードする際に設定をそのままコピーしていたのが原因でした。

3
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?