Detecting Closed Sockets

Peter Whittaker pww at bnr.ca
Sat Mar 23 02:06:48 AEST 1991


In article <63357 at bbn.BBN.COM> fkittred at spca.bbn.com (Fletcher Kittredge) writes:
>In article <39989 at cup.portal.com> LordBah at cup.portal.com (Jeffrey J Vanepps) writes:
>>I have a problem with telling when the other side of a socket connection
>>has exited.  Normally a read/recv which returns zero after select(2) says
>>that there is data to be read signifies a closed connection.  In my case,
>>the application can't use this technique.  Here's the situation:
>
>So why don't you set SO_KEEPALIVE on the socket and respond to the SIGIO?
>
oooo, my least favourite topic :->

>Note that doing your own polling is considered by many to be a more elegant
>solution than setting SO_KEEPALIVE.  SO_KEEPALIVE causes additional network
>load.

That's not all it does!  Beware: certain OS's have broken KEEPALIVE handling,
notably HP-UX pre-7.0.5 (it seems to be fixed in 7.0.5), and (maybe)
SunOS pre 4.1.1.  The nature of the problem is this:

(Assume TCP connection between any Sun or HP server, and an HPUX 6.5 client):

The client sets KEEPALIVE;  the server crashes;  the server's kernel tries
to close the connection gracefully and sends a RST;  the client ACKs the RST,
and should then wait ~15 minutes before sending its own RST - which the server
side kernel should ACK, whereupon the connection is closed.

What actually happens:  the client receives the RST, ACKs it, then starts
waiting the 15 minutes.  Before the 15 minutes are up, THE CLIENT SENDS
a KEEPALIVE (i.e. an ACK!).  The server ACKs the KEEPALIVE.  Result?
The connection lives forever!

Note that this buggy behaviour depends on the server ACK the KEEPALIVE 
even though it has sent a RST!  Unfortunately, no one seems to have accounted
for that possibility!  (Not surprising:  once you send a RST, the 'other side'
should - one would think - stop KEEPALIVING!  Of course, KEEPALIVE is not TCP!).

Beware the SO_KEEPALIVE, my son, and keep your vorpal ready!
(in other words, use select()) 


--
Peter Whittaker      [~~~~~~~~~~~~~~~~~~~~~~~~~~]   Open Systems Integration
pww at bnr.ca           [                          ]   Bell Northern Research 
Ph: +1 613 765 2064  [                          ]   P.O. Box 3511, Station C
FAX:+1 613 763 3283  [__________________________]   Ottawa, Ontario, K1Y 4H7



More information about the Comp.unix.programmer mailing list