From: Bill Kelly Date: 2006-03-30T02:20:34+09:00 Subject: Re: timeout when listening with TCPServer From: "Shea Martin" > > Thread.new do > > begin > l_exit_status = false > > l_listener = TCPServer.new( HOST, p_port ) > l_socket = false > > Timeout.timeout( 2 ) do > l_socket = l_listener.accept > end #timeout > > #use socket > l_socket.put "send you requested data" > > l_exit_status = true > > rescue SocketError > $log.puts "socket error" > l_exit_status = false > rescue Timeout::Error > $log.puts "you only get 2 seconds to come and get the data" > l_exit_status = false > rescue Errno::EBADF > $log.puts "could not open connection" > l_exit_status = false > ensure > l_socket.close if l_socket > end #begin/rescue > > l_exit_status > end #thread/do > > > I don't think I have a race condition here, do I? I'm not 100% sure myself, because I don't know whether this assignment: l_socket = l_listener.accept ^^^ is atomic, or not. However, I'm not suggesting to worry about it, either. Practically speaking it seems unlikely to be a problem, and ruby's garbage collector will generally recover orphaned file handles. Regards, Bill