Fixing 'Address already in use' Errors in Mongrel

Posted on December 18, 2007

This problem was truly maddening. Some kind of rogue tcp process spawned by mongrel was not getting closed properly.

It was truly a ghost in the machine.

Doing a ‘ps aux | grep ruby’ or ‘ps aux | grep 3000’ yielded nothing. So it was darn neigh impossible to find a process to kill that was somehow still holding onto the socket. Attempting to start mongrel yielded the “Address already in use – bind(2) (Errno::EADDRINUSE)” error.

This link provided the answer.

If you normally run your mongrel(s) on port 3000, use the following snippet to kill em dead:

kill -HUP `lsof -t -i TCP:3000`

The following is the complete stack trace:

** Ruby version is not up-to-date; loading cgi_multipart_eof_fix
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
Exiting
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/tcphack.rb:12:in `initialize_without_backlog': Address already in use - bind(2) (Errno::EADDRINUSE)
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/tcphack.rb:12:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel.rb:92:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/configurator.rb:139:in `listener'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/bin/mongrel_rails:99:in `cloaker_'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/configurator.rb:50:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/bin/mongrel_rails:84:in `run'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/command.rb:212:in `run'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/bin/mongrel_rails:281
         ... 6 levels...

Good luck!

Comments
  1. John PlumridgeJanuary 24, 2008 @ 05:42 PM

    hi thanks for that