Why do Windows sockets break after exactly 4 minutes?

Geoffrey Talvola gtalvola at nameconnector.com
Fri Mar 22 14:18:22 EST 2002


Peter Hansen wrote:
> Geoffrey Talvola wrote:
> > 
> > I've noticed that Windows sockets seem to close all by 
> themselves if there
> > is no activity for 4 minutes.  Does anyone know why?  Is it 
> possible to
> > control this behavior?  Just a link to some documentation 
> of this behavior
> > would be greatly appreciated.
> > 
> > I don't think the same thing happens on Linux, or maybe it 
> does but the
> > timeout is longer than 4 minutes there.
> > 
> > I've attached a set of 2 test scripts that provoke the 
> behavior.  Start up
> > server.py first, then client.py, then wait 4 minutes.
> 
> Haven't run your tests, but isn't this what SO_KEEPALIVE is used for?
> 
> 
> Try using:
> 
> import socket
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
> 
> and this will turn on the keep-alive feature (I think... pulled
> that last line from pygale.py since I don't know where it
> is well documented).

I tried that after I sent the original message.  But after putting in calls
to turn on keepalive in every place and combination I could think of, it
still made no difference.  If the time.sleep() is for less than 4 minutes,
everything works, but if it's > 4 minutes, a socket.error exception gets
thrown on the client side.

- Geoff




More information about the Python-list mailing list