Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recovering from a broken TCP socket in Ruby when in gets()

I'm reading lines of input on a TCP socket, similar to this:

class Bla  
  def getcmd
    @sock.gets unless @sock.closed?
  end

  def start     
    srv = TCPServer.new(5000)
    @sock = srv.accept
    while ! @sock.closed?
      ans = getcmd
    end
  end
end

If the endpoint terminates the connection while getline() is running then gets() hangs.

How can I work around this? Is it necessary to do non-blocking or timed I/O?

like image 604
asussex Avatar asked Sep 14 '08 21:09

asussex


2 Answers

You can use select to see whether you can safely gets from the socket, see following implementation of a TCPServer using this technique.

require 'socket'

host, port = 'localhost', 7000

TCPServer.open(host, port) do |server|
  while client = server.accept
    readfds = true
    got = nil
    begin
      readfds, writefds, exceptfds = select([client], nil, nil, 0.1)
      p :r => readfds, :w => writefds, :e => exceptfds

      if readfds
        got = client.gets 
        p got
      end
    end while got
  end
end

And here a client that tries to break the server:

require 'socket'

host, port = 'localhost', 7000

TCPSocket.open(host, port) do |socket|
  socket.puts "Hey there"
  socket.write 'he'
  socket.flush
  socket.close
end
like image 78
manveru Avatar answered Nov 09 '22 07:11

manveru


The IO#closed? returns true when both reader and writer are closed. In your case, the @sock.gets returns nil, and then you call the getcmd again, and this runs in a never ending loop. You can either use select, or close the socket when gets returns nil.

like image 42
Roman Avatar answered Nov 09 '22 08:11

Roman