Feature #6583

Improve socket exception message when bind() fails

Added by Eric Hodel almost 2 years ago. Updated about 1 year ago.

[ruby-core:45617]
Status:Closed
Priority:Normal
Assignee:Akira Tanaka
Category:ext
Target version:next minor

Description

=begin
If the user tries to bind a port that is already in use the user does not receive any notification of which port (and address) failed. When multiple connections are being made makes it difficult to determine which connection was not correctly bound.

For example, WEBrick binds both IPv4 and IPv6 sockets. If one of the ports is already bound you will receive a confusing error message.

This patch adds the port and address to the error message:

$ cat test.rb
require 'socket'

TCPServer.new '0.0.0.0', 5900
$ make runruby
./miniruby -I./lib -I. -I.ext/common ./tool/runruby.rb --extout=.ext -- --disable-gems ./test.rb
./test.rb:3:in initialize': Address already in use - bind(2) for "0.0.0.0" port 5900 (Errno::EADDRINUSE)
from ./test.rb:3:in
new'
from ./test.rb:3:in `'
make: *** [runruby] Error 1

Current behavior:

$ ruby test.rb
test.rb:3:in initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
from test.rb:3:in
new'
from test.rb:3:in `'

=end

socket.improve_bind_error.patch Magnifier (5.3 KB) Eric Hodel, 06/13/2012 11:42 AM

socket.improve_bind_error.2.patch Magnifier (12.7 KB) Eric Hodel, 07/04/2012 11:07 AM

Associated revisions

Revision 40149
Added by Akira Tanaka about 1 year ago

  • ext/socket: Improve socket exception message to show socket address.
    [Feature #6583] proposed Eric Hodel.

  • ext/socket/rubysocket.h (rsocksysfailhostport): Declared.
    (rsocksysfailpath): Ditto.
    (rsock
    sysfailsockaddr): Ditto.

  • ext/socket/udpsocket.c (udpconnect): Use rsocksysfailhostport.
    (udp
    bind): Ditto.
    (udp_send): Ditto.

  • ext/socket/init.c (rsockinitsock): Specify a string for rbsysfail
    argument.
    (makefdnonblock): Ditto.
    (rsocksaccept): Ditto.

  • ext/socket/ipsocket.c (initinetsockinternal): Use
    rsocksysfailhostport.

  • ext/socket/socket.c (rsocksysfailhostport): Defined.
    (rsocksysfailpath): Ditto.
    (rsock
    sysfailsockaddr): Ditto.
    (setupdomainandtype): Use rsocksysfailsockaddr.
    (sockconnectnonblock): Ditto.
    (sockbind): Ditto.
    (sock
    gethostname): Specify a string for rbsysfail argument.
    (socketsipaddresslist): Ditto.

  • ext/socket/basicsocket.c (bsockshutdown): Specify a string for
    rb
    sysfail argument.
    (bsock
    setsockopt): Use rsocksysfailpath.
    (bsock
    getsockopt): Ditto.
    (bsockgetpeereid): Refine the argument for rbsys_fail.

  • ext/socket/unixsocket.c (rsockinitunixsock): Use
    rsocksysfailpath.
    (unix
    path): Ditto.
    (unixsendio): Ditto.
    (unixrecvio): Ditto.
    (unixaddr): Ditto.
    (unix
    peeraddr): Ditto.

History

#1 Updated by Eric Hodel almost 2 years ago

This updated patch adds three new methods rsocksysfailhostport, rsocksysfailpath and rsocksysfailsockaddr for raising exceptions from more than bind().

Additionally, rbsysfail() calls were audited and names of system calls were added where they were missing, and updated where incomplete (lacking manual page designator).

#2 Updated by Akira Tanaka almost 2 years ago

  • Assignee set to Akira Tanaka

#3 Updated by Yusuke Endoh almost 2 years ago

  • Status changed from Open to Assigned

#4 Updated by Akira Tanaka over 1 year ago

  • Target version changed from 2.0.0 to next minor

#5 Updated by Akira Tanaka about 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r40149.
Eric, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • ext/socket: Improve socket exception message to show socket address.
    [Feature #6583] proposed Eric Hodel.

  • ext/socket/rubysocket.h (rsocksysfailhostport): Declared.
    (rsocksysfailpath): Ditto.
    (rsock
    sysfailsockaddr): Ditto.

  • ext/socket/udpsocket.c (udpconnect): Use rsocksysfailhostport.
    (udp
    bind): Ditto.
    (udp_send): Ditto.

  • ext/socket/init.c (rsockinitsock): Specify a string for rbsysfail
    argument.
    (makefdnonblock): Ditto.
    (rsocksaccept): Ditto.

  • ext/socket/ipsocket.c (initinetsockinternal): Use
    rsocksysfailhostport.

  • ext/socket/socket.c (rsocksysfailhostport): Defined.
    (rsocksysfailpath): Ditto.
    (rsock
    sysfailsockaddr): Ditto.
    (setupdomainandtype): Use rsocksysfailsockaddr.
    (sockconnectnonblock): Ditto.
    (sockbind): Ditto.
    (sock
    gethostname): Specify a string for rbsysfail argument.
    (socketsipaddresslist): Ditto.

  • ext/socket/basicsocket.c (bsockshutdown): Specify a string for
    rb
    sysfail argument.
    (bsock
    setsockopt): Use rsocksysfailpath.
    (bsock
    getsockopt): Ditto.
    (bsockgetpeereid): Refine the argument for rbsys_fail.

  • ext/socket/unixsocket.c (rsockinitunixsock): Use
    rsocksysfailpath.
    (unix
    path): Ditto.
    (unixsendio): Ditto.
    (unixrecvio): Ditto.
    (unixaddr): Ditto.
    (unix
    peeraddr): Ditto.

Also available in: Atom PDF