is SO_KEEPALIVE broken?

Discussion in 'Linux Networking' started by Johann Klammer, Oct 6, 2012.

  1. Hello,
    I have two programs, which communicate via a TCP socket. one client
    which sends one-byte commands and a server which interprets them. The
    server uses read() to get those bytes.

    The manual page for recv says:
    (allegedly recv and read behave roughly the same)

    So, I've been using the return value to determine if the connection
    should end. Unfortunately it turns out, that in rare cases 0 is returned
    even for live connections, which will unexpectedly terminate the session!!
    The connection is using the SO_KEEPALIVE TCP option, to avoid problems
    with stale connections after computer crashes. AFAIK it did not happen
    before introduction of the SO_KEEPALIVE into the program.

    So, the obvious question is now: How may I reliably recognize a close on
    the other side of the connection (to avoid stale connection threads),
    while still using keepalive to catch those no-FIN situations.

    regards,
    JK
     
    Johann Klammer, Oct 6, 2012
    #1
    1. Advertisements

  2. Johann Klammer

    telsar Guest

    select until true on the filehandle and when there is something to recv
    it would be true.
     
    telsar, Oct 6, 2012
    #2
    1. Advertisements

  3. I have been using poll with .events=POLLIN|POLLPRI and checked for
    POLLHUP and POLLIN. I thought that I'd get at least one byte from read
    after the POLLIN showed up, but in at least one case that was not true.
    Not sure why. Tomorrow, I'll try to reproduce it.
     
    Johann Klammer, Oct 6, 2012
    #3
  4. Implementations vary wildly (and even within themselves); you need to do
    the read/recv, you can’t rely on just checking POLLHUP.

    http://www.greenend.org.uk/rjk/tech/poll.html collects some of the
    variation.
     
    Richard Kettlewell, Oct 7, 2012
    #4
  5. Sorry, cannot seem to reproduce it. Perhaps something else is broken in
    that program...
    Thanks, I'll read it.
     
    Johann Klammer, Oct 8, 2012
    #5
  6. The following message is a courtesy copy of an article
    that has been posted to comp.os.linux.networking as well.


    Johann> Hello, I have two programs, which communicate via a TCP
    Johann> socket. one client which sends one-byte commands and a server
    Johann> which interprets them. The server uses read() to get those
    Johann> bytes.

    Johann> The manual page for recv says:

    Johann> (allegedly recv and read behave roughly the same)

    Shouldn't both just block until there's something to read? Or do you
    turn on nonblocking mode?

    --
    Ian Zimmerman
    gpg public key: 1024D/C6FF61AD
    fingerprint: 66DC D68F 5C1B 4D71 2EE5 BD03 8A00 786C C6FF 61AD
    http://www.gravatar.com/avatar/c66875cda51109f76c6312f4d4743d1e.png
    Rule 420: All persons more than eight miles high to leave the court.
     
    Ian Zimmerman, Oct 14, 2012
    #6
  7. Yes, unless the connection has been closed, in which case they return
    zero(at least here).

    Sorry about the line noise. The bug turned out to be in the client which
    shut down unexpectedly. Had nothing to do with recv()/KEEPALIVE.

    bye,
    JK
     
    Johann Klammer, Oct 18, 2012
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.