From: Florian Frank Date: 2002-05-27T07:01:43+09:00 Subject: Possible Bug in Ruby? Hi! I have observed a very strange behavior of Ruby (1.6.7). I think it has something to do with threads and sockets. I have written a little script that can be used to reproduce the problem: require 'timeout' require 'socket' host, port = ARGV.shift.split(/:/) print "My PID is #$$.\n" s = nil print "Connecting...\n" begin timeout(5) do s = TCPSocket.new(host, port) line = s.readline.chomp print "Got line = '#{line}'.\n" end rescue TimeoutError print "Timed out!\n" rescue print "Caught exception #{$!.type}: #$!!\n" ensure print "s = #{s}.\n" s.close if s end print "Press key to exit.\n" gets There are four possible cases to connect to a port on some host using this script (with $ ruby connect.rb host:port). 1. The port is closed. Then an Errno::EINVAL exception is thrown. 2. The port is opened and the listening server (perhaps a sshd on port 22) sends a line which is received. The socket s is closed in the ensure code block. 3. The port is opened and the listening server (e. g. a httpd on port 80) doesn't send anything but waits for a request from the client. The connection times out after 5 seconds and the socket s is closed in the ensure code block. 4. The host is not reachable because it is not on the wire. In this case the connection times out and the socket s is nil and thus cannot be closed. This would be ok but if you use a program like lsof or fstat and type "lsof -p $PID" or "fstat -p $PID" you can see that the socket is in fact opened. It doesn't seem possible to close it anyhow from within this script. I don't know where the problem lies but I don't think it has something to do with timeout.rb but with the thread it uses. Does somebody have an idea how to fix this?