Issue #9247 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 1.9.3: REQUIRED, 2.0.0: REQUIRED to 1.9.3: REQUIRED, 2.0.0: DONE

r44184 was backported to ruby_2_0_0 at r44390.
----------------------------------------
Bug #9247: Bugs in socket.rb (exception retrieval)
https://bugs.ruby-lang.org/issues/9247#change-43872

Author: ko1 (Koichi Sasada)
Status: Assigned
Priority: Normal
Assignee: nagachika (Tomoyuki Chikanaga)
Category: lib
Target version: current: 2.1.0
ruby -v: ruby 2.1.0dev (2013-12-13 trunk 44170) [i386-mswin32_110]
Backport: 1.9.3: REQUIRED, 2.0.0: DONE


Ruby 2.1 (trunk) 2.0 and 1.9.3 both has a bug to retrieve exception.
In the following code, opened socket is always closed.

require 'socket'
ai = Addrinfo.tcp(nil, 12345)
begin
  raise
rescue
  p ai.listen #=> #<Socket:(closed)>
end

This is because in socket.rb, there are such error handling code:

  # creates a listening socket bound to self.
  def listen(backlog=Socket::SOMAXCONN)
    sock = Socket.new(self.pfamily, self.socktype, self.protocol)
    begin
      sock.ipv6only! if self.ipv6?
      sock.setsockopt(:SOCKET, :REUSEADDR, 1)
      sock.bind(self)
      sock.listen(backlog)
      if block_given?
        yield sock
      else
        sock
      end
    ensure
      ### THIS LINE CLOSE `sock' every time when $! is not nil
      sock.close if !sock.closed? && (block_given? || $!)
    end
  end

There are similar code in socket.rb.

akr-san already knows this issue.
This ticket is to note issues.


We realize this issue because webrick use this feature from 2.1.
The following code no longer works on 2.1.

  begin
    require 'xyzzy' # something like webrick
  rescue LoadError
    require 'webrick' 
    server = WEBrick::HTTPServer.new(Port: 12345)
  end

Because $! is an instance of LoadError.



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