Search code examples
rubytcpsocket

stream closed (IOError) when closing Ruby TCPSocket client


I've got a Ruby TCPSocket client that works great except when I'm trying to close it. When I call the disconnect method in my code below, I get this error:

./smartlinc.rb:70:in `start_listen': stream closed (IOError)
    from ./smartlinc.rb:132:in `initialize'
    from ./smartlinc.rb:132:in `new'
    from ./smartlinc.rb:132:in `start_listen'
    from bot.rb:45:in `initialize'
    from bot.rb:223:in `new'
    from bot.rb:223

Here's the (simplified) code:

class Smartlinc

    def initialize
        @socket = TCPSocket.new(HOST, PORT)
    end

    def disconnect
        @socket.close
    end

    def start_listen
        # Listen on a background thread
        th = Thread.new do
            Thread.current.abort_on_exception = true

            # Listen for Ctrl-C and disconnect socket gracefully.
            Kernel.trap('INT') do 
                self.disconnect
                exit
            end

            while true
                ready = IO.select([@socket])
                readable = ready[0]
                readable.each do |soc|
                    if soc == @socket
                        buf = @socket.recv_nonblock(1024)
                        if buf.length == 0
                            puts "The socket connection is dead. Exiting."
                            exit
                        else
                            puts "Received Message"
                        end
                    end
                end # end each
            end # end while

        end # end thread
    end # end message callback

end

Is there a way I can prevent or catch this error? I'm no expert in socket programming (obviously!), so all help is appreciated.


Solution

  • Your thread is sitting in IO.select() while the trap code happily slams the door in its face with @socket.close, hence you get some complaining.

    Don't set abort_on_exception to true, or then handle the exception properly in your code:
    Something along these lines...

    Kernel.trap('INT') do
      @interrupted = true
      disconnect
      exit
    end
    
    ...
    ready = nil
    begin
      ready = IO.select(...)
    rescue IOError
      if @interrupted
        puts "Interrupted, we're outta here..."
        exit
      end
      # Else it was a genuine IOError caused by something else, so propagate it up..
      raise
    end
    
    ...