std.socket - problems closing socket

Regan Heath regan at netmail.co.nz
Mon Oct 3 02:36:16 PDT 2011


On Sat, 01 Oct 2011 00:26:35 +0100, simendsjo <simendsjo at gmail.com> wrote:

> Not sure if this is a problem with std.socket, nginx or my knowledge of  
> sockets. I'm pretty sure it's the last one.
>
> I'm experimenting with fastcgi on nginx, and the socket stays in  
> TIME_WAIT even after I call
>    socket.shutdown(SocketShutdown.BOTH);
>    socket.close();
>
> (Crossposted from SO:  
> http://stackoverflow.com/questions/7616601/nginx-fastcgi-and-open-sockets)

For a "graceful" close you're supposed to ensure there is no data  
pending.  To do that you:

shutdown(SD_SEND);  // send only, not recv
<enter a loop reading all data remaining on the socket>
close();

The loop should read until recv returns 0.  If recv returns -1 and the  
socket is blocking it should error/exit.  If recv returns -1 and the  
socket is non-blocking it should check for [WSA]EWOULDBLOCK (and  
select/sleep + loop) or error/exit.

The reason to do this is to flush all the data from the socket buffers on  
the remote and local ends, otherwise a close can cause remote buffered  
data to cause a "connection broken" error on the remote end, and/or (I am  
guessing a little here) may cause the socket to close while negotiating a  
graceful close, and/or remain in a TIME_WAIT state due to buffered data or  
data "in flight".

.. are you setting any close options/timeouts i.e. LINGER?

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list