Issue #7100 has been updated by ChultOch5 (Sho Morita).


=begin
腱???????????с?????罕???????吟????冴?????障????????????????c??茘???????冴????翫?????WEBrick ??泣?若????若?? IPv6 ??с?≪??祉?鴻?с????????c?????障????障???????障???????医????????c??? http://localhost:3000/ ?????????? localhost ??????絎??????????≪??祉?剛????純???c?????障????障?????(:BindAddress ?????ず???????絎?????????医?????????с???????????????)

WEBrick ???????ャ?<?潟?????? :BindAddress ???≪?????

  ?????????????? nil ??? "0.0.0.0", "::" ????????絎?????????翫????篏???純?????鴻?????????????若??ゃ?潟?帥?若????с?若?鴻?絲?????? listen ??????紮??????障?????

???吾?????????????????絎????????

* nil ??????絎?????????篏???純?????鴻??? IPv4 ????????? IPv6 ????????????若??ゃ?潟?帥?若????с?若?鴻?絲?????? listen ?????????
* "0.0.0.0" ??????絎?????????篏???純?????鴻??? IPv4 ????????????若??ゃ?潟?帥?若????с?若?鴻?絲???????? listen ?????????
* "::" ??????絎?????????篏???純?????鴻??? IPv6 ????????????若??ゃ?潟?帥?若????с?若?鴻?絲???????? listen ?????????(筝????激?鴻???????с?? nil ????絎????????????罕??????????鴻??? IPv4 ??? IPv6 ????????????若??ゃ?潟?帥?若????с?若?鴻?絲?????? listen ??????)

???????障?????

筝?????激?鴻?????(Linux????)??с???IPv6 ???ゃ??????若????≪?????鴻?с????? :: ??? bind ??????????IPv6 ????????????若??ゃ?潟?帥?若????с?若?鴻???????с??????IPv4 ????????????若??ゃ?潟?帥?若????с?若?鴻?? bind ????????障?????0.0.0.0 ??? :: ???筝≧?? bind ????????????????????緇??????? bind ????????鴻?? Address already in use(EADDRINUSE) ????若????紊掩?????????????????IPv6 ??с??≪??祉?鴻??茵??????????c?????障????障?????

* http://www.a-k-r.org/pub/socket-rubykaigi2009.pdf
* http://codezine.jp/article/detail/5395
* NEWS-1.9.2

?????????????Ruby 1.9.2 ??? Socket ????????????罘???純??菴遵???????????IPv6 ??????????????倶?劫???????????????????с?????????????ц申???????????? Socket#ipv6only! ?????若?喝?冴????? IPV6_V6ONLY ??純?宴???????????激?с?潟???????鴻???????0.0.0.0 ??? :: ??筝??鴻?? bind ??????篋??????с??????????????????障?????

??????????????? WEBrick ???篏睡?????????? TCPServer ??????ャ??????????? API ??с??IPV6_V6ONLY ??純?宴???????????激?с?潟????祉??????????障??????????違????? API ??с????? Socket.tcp_server_sockets ??????綽?荀??????????? Socket#ipv6only! ?????若?喝?冴??????????障?????

??с?????с??TCPServer.new ??с???? Socket.tcp_server_sockets ???篏帥????????????????????????????????c?????с????????TCPServer.new ???????吾?с?????? Socket.tcp_server_sockets ???????吾?с????????????????с??<??????????????????臀??????????????障????? WEBrick ?????????????√?????純????????с?≪?т???????у?????莎激????????с???????綽??????с?????

????????с??篋??????с??????莎激?????????????????? TCPServer ???篏帥??????膓???????ゃ?ゅ?????篆罩????????????????????

* :BindAddress == nil ??с???????翫??????:: ????????? bind ??????0.0.0.0 ???緇??????? bind (??????? EADDRINUSE ???莎激????????∴??)?????????

??????????????????????域??????с????????????????障?????筝?綽?????????????篏???c??帥?障????????

  Index: lib/webrick/utils.rb
  ===================================================================
  --- lib/webrick/utils.rb	(revision 37168)
  +++ lib/webrick/utils.rb	(working copy)
  @@ -79,6 +79,15 @@
                                   Socket::AI_PASSIVE)  # flag
         last_error = nil
         sockets = []
  +      # If address == nil, Socket.getaddrinfo returns 2 entries, the
  +      # IPv4 wildcard address "0.0.0.0" and the IPv6 wildcard address "::".
  +      # On some systems, if you try to bind for both "0.0.0.0" and "::",
  +      # the later one will fails.
  +      # To workaround such behaviour, try bind for "::" first, then
  +      # bind "0.0.0.0" and ignore EADDRINUSE error.
  +      if address.nil?
  +        res = res.sort_by{|i| i[4]}.reverse
  +      end
         res.each{|ai|
           begin
             logger.debug("TCPServer.new(#{ai[3]}, #{port})") if logger
  @@ -87,7 +96,9 @@
             Utils::set_close_on_exec(sock)
             sockets << sock
           rescue => ex
  -          logger.warn("TCPServer Error: #{ex}") if logger
  +          logger.warn("TCPServer Error: #{ex}") if logger and !(address.nil? and
  +                                                                ai[4] == Socket::AF_INET and
  +                                                                Errno::EADDRINUSE === ex)
             last_error  = ex
           end
         }


??<??帥?????号?????茹f浦???????障???????????

  # sysctl net.ipv6.bindv6only=1

???????????IPV6_V6ONLY ??純?宴???????????激?с?潟???????????????ф????鴻???????:BindAddress ??? nil ??с??茘?????冴????????障?????

=end
----------------------------------------
Bug #7100: WEBrick::HTTPServer.new ??? BindAddress ??????絎??????????翫?????????茘?????荐???蚊????????
https://bugs.ruby-lang.org/issues/7100#change-30447

Author: sho-h (Sho Hashimoto)
Status: Open
Priority: Low
Assignee: 
Category: 
Target version: 
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]


=begin
篁ヤ???????????????????????茘?????荐???蚊????????????????с?????

  $ ruby -v -r webrick -e 'WEBrick::HTTPServer.new(Port: 3000)'
  ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
  [2012-09-04 19:20:48] INFO  WEBrick 1.3.1
  [2012-09-04 19:20:48] INFO  ruby 1.9.3 (2012-04-20) [x86_64-linux]
  [2012-09-04 19:20:48] WARN  TCPServer Error: Address already in use - bind(2)

1.8 ??с?????蚊???????障???????с????????1.9.1 篁ラ????????蚊???????障????????

lib/webrick/utils.rb ??? WEBrick::Utils#create_listeners ???篁ヤ?????????????c?????????

  res = Socket::getaddrinfo(address, port,
                            Socket::AF_UNSPEC,   # address family
                            Socket::SOCK_STREAM, # socket type
                            0,                   # protocol
                            Socket::AI_PASSIVE)  # flag
  last_error = nil
  sockets = []
  res.each{|ai|
    begin
      logger.debug("TCPServer.new(#{ai[3]}, #{port})") if logger
      sock = TCPServer.new(ai[3], port)
      ...

Socket.getaddrinfo ??? 1.9 ??????茲???医?ゃ??菴???????????????????с?????1.8.7 ?????? 0.0.0.0 ???鴻???????с????????

  $ ruby -v -r pp -r socket -e 'pp Socket::getaddrinfo(nil, 3000, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)'
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
  [["AF_INET", 3000, "0.0.0.0", "0.0.0.0", 2, 1, 6],
   ["AF_INET6", 3000, "::", "::", 10, 1, 6]]

WEBrick::Utils#create_listeners ???潟?<?潟???????????????????????????<??????????????????с????????config[:BindAddress] ??????????????ゃ?? 0.0.0.0 ??? :: ????<???????????????障???????????????с???????????
=end



-- 
http://bugs.ruby-lang.org/