Why do Windows sockets break after exactly 4 minutes?

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


Gordon McMillan 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.
> 
> Does it help to note that if you change:
> 
> >     time.sleep(250)
> 
> to time.sleep(15), then the socket closes after 15 seconds?
> 
> Your client does a shutdown(1) after sending some data.
> The shutdown(1) causes the server to fall out of the
> receive loop. It then sleeps for however long specified,
> sends some data and closes the socket.
> 
> In other words, the behavior is exactly what you asked
> for, and Windows' socket implementation has nothing to
> do with it.

But that's not what's happening.  If I have the call to time.sleep() sleep
any amount of time up to 4 minutes, then the client receives the "goodbye"
message after the correct amount of time -- exactly as intended.  But once I
bump the time up to > 4 minutes, then instead of receiving the "goodbye"
message from the server, a socket.error exception gets raised in client.py
_exactly_ 4 minutes after the call to shutdown (even if I use
time.sleep(1000) or time.sleep(10000)):

sent message at Fri Mar 22 14:14:01 2002
socket exception caught at Fri Mar 22 14:18:01 2002
Traceback (most recent call last):
  File "D:\junk\SocketTimeoutTest\client.py", line 13, in ?
    data = s.recv(1024)
  File "<string>", line 1, in recv
socket.error: (10054, 'Connection reset by peer')

When I use a time shorter than 4 minutes in the time.sleep(), the output
from client.py looks like:

sent message at Fri Mar 22 14:20:43 2002
Received 15 bytes: Goodbye, world!
closed socket at Fri Mar 22 14:20:58 2002

So something is breaking the socket after 4 minutes.

I should note that this is on a Windows NT 4.0 SP6 box.  Perhaps I'll try
Win2K to see if there's a difference there.

- Geoff




More information about the Python-list mailing list