TCP Socket: Connection reset, but Select says it's valid

Discussion in 'Ruby' started by Scott Cole, Sep 28, 2010.

  1. Scott Cole

    Scott Cole Guest

    I'm running a server that sometimes encounters a
    connection-reset-by-peer error when reading data from a client. I'm
    calling IO.Select to verify that there are characters to be read, but
    occasionally the "rescue" below is activated when I do my gets. I
    presume this means the socket was closed after Select noticed there were
    characters to be read but before I actually try to read them.

    The client and server are in the same machine, so there's no cable to
    pull out.

    The messages of interest are sent every 30 seconds, and there might be 1
    or 2 failures in a day. Running on Mac OS X 10.5.6.

    The problem seems to occur when the client (below) connects to send a
    message and another client connects before the first client's
    transaction is complete. This transaction takes about 1 second. The
    specified timeout is 120 seconds.

    Is there anything obvious wrong with my code? I omitted quite a few
    lines, I hope not too much.

    Thanks-
    Scott

    ######################### Server (client follows)
    #########################

    require 'gserver'

    class ChatServer < GServer # Server class derived from GServer super
    class
    def initialize(*args)
    super(*args)
    # Keep a record of the client IDs allocated
    # and the lines of chat
    @@client_id = 0
    @@chat = []
    end

    def serve(io) # Serve method handles connections
    # Increment the client ID so each client gets a unique ID
    @@client_id += 1
    my_client_id = @@client_id
    my_position = @@chat.size


    loop do
    #Every n seconds check for data
    n = 0.5

    selection = IO.select([io], nil, [io], n)
    # If some event occurred, retrieve the data and process it...
    if selection && selection[0] then
    # There was a read event
    begin
    line = io.gets
    rescue Exception => e
    stat = ''
    puts "\nerror reading 'line' from #{io}"
    puts "#{ e } (#{ e.class })!"
    # Close socket
    io.close
    print("\nSelection is #{selection[0]}\n")
    end
    if selection[2].size > 0 then
    puts "%%%%%%%% Select error array is #{selection[2]}" +
    Time.now.to_s
    end
    Scott Cole, Sep 28, 2010
    #1
    1. Advertising

  2. Scott Cole wrote:
    > I'm running a server that sometimes encounters a
    > connection-reset-by-peer error when reading data from a client. I'm
    > calling IO.Select to verify that there are characters to be read, but
    > occasionally the "rescue" below is activated when I do my gets.


    Your code prints the exception class and message, what do you see?
    I
    > presume this means the socket was closed after Select noticed there were
    > characters to be read but before I actually try to read them.


    select returns true for an EOF-condition too. From `man 2 select`:

    Three independent sets of file descriptors are watched. Those
    listed
    in readfds will be watched to see if characters become
    available for
    reading (more precisely, to see if a read will not block; in
    particuâ€
    lar, a file descriptor is also ready on end-of-file)
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 28, 2010
    #2
    1. Advertising

  3. Scott Cole

    Scott Cole Guest

    Thanks for responding. I would expect to get some kind of EOF error on
    an EOF condition. Instead I see something like

    error reading 'line' from #<TCPSocket:0x40e3f4>
    Connection reset by peer (Errno::ECONNRESET)!

    Selection is #<TCPSocket:0x40e3f4>



    Brian Candler wrote:
    > Scott Cole wrote:
    >> I'm running a server that sometimes encounters a
    >> connection-reset-by-peer error when reading data from a client. I'm
    >> calling IO.Select to verify that there are characters to be read, but
    >> occasionally the "rescue" below is activated when I do my gets.

    >
    > Your code prints the exception class and message, what do you see?
    > I
    >> presume this means the socket was closed after Select noticed there were
    >> characters to be read but before I actually try to read them.

    >
    > select returns true for an EOF-condition too. From `man 2 select`:
    >
    > Three independent sets of file descriptors are watched. Those
    > listed
    > in readfds will be watched to see if characters become
    > available for
    > reading (more precisely, to see if a read will not block; in
    > particuâ€
    > lar, a file descriptor is also ready on end-of-file)


    --
    Posted via http://www.ruby-forum.com/.
    Scott Cole, Sep 28, 2010
    #3
  4. Scott Cole wrote:
    > Thanks for responding. I would expect to get some kind of EOF error on
    > an EOF condition. Instead I see something like
    >
    > error reading 'line' from #<TCPSocket:0x40e3f4>
    > Connection reset by peer (Errno::ECONNRESET)!


    Yep, it's not a simple EOF - the far end has sent a TCP RST instead of a
    FIN - but AFAIK the socket is marked 'readable' for select.

    More info at:
    http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2007-08/msg00176.html

    Note also that this is does not mean that the socket is marked
    selectable for 'error' - selection[2]. In fact, you're almost certainly
    never going to find the socket marked that way. See
    http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2007-11/msg00187.html
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 28, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kathryn Bean
    Replies:
    1
    Views:
    672
    John Harrison
    Oct 28, 2004
  2. Tiger
    Replies:
    5
    Views:
    951
    Dave Thompson
    May 1, 2006
  3. Helene Unterwieser
    Replies:
    14
    Views:
    3,643
    Esmond Pitt
    Jul 3, 2007
  4. Object01

    TCP reset caused by socket.py

    Object01, Dec 11, 2007, in forum: Python
    Replies:
    10
    Views:
    1,206
    Gabriel Genellina
    Dec 13, 2007
  5. Replies:
    13
    Views:
    233
Loading...

Share This Page