[Python-Dev] Minor socket timeout quibble - timeout raises socket.error

Guido van Rossum guido@python.org
Fri, 12 Jul 2002 12:37:10 -0400


[Skip Montanaro]
> > I just noticed in the development docs that when a timeout on a socket
> > occurs, socket.error is raised.  I rather liked the idea that a different
> > exception was raised for timeouts (I used Tim O'Malley's timeout_socket
> > module).  Making a TimeoutError exception a subclass of socket.error would
> > be fine so you can catch it with existing code, but I could see recovering
> > differently for a timeout as opposed to other possible errors:
> > 
> >     sock.settimeout(5.0)
> >     try:
> >         data = sock.recv(8192)
> >     except socket.TimeoutError:
> >         # maybe requeue the request
> >         ...
> >     except socket.error, codes:
> >         # some more drastic solution is needed
> >         ...
> > 

[Bernard Yue]
> +1 on your suggestion.  Anyway, under windows, the current
> implementation returns incorrect socket.error code for timeout.  I am
> working on the test suite as well as a fix for problem found.  Once the
> code is bug free maybe we can put the TimeoutError in.
> 
> I will leave it to Guido for the approval of the change.  When he comes
> back from his holiday.

The way I restructured the code it is impossible to distinguish a
timeout error from other errors; you simply get the "no data
available" error from the socket operation.  This is the same error
you'd get in non-blocking mode.

Before I recomplicate the code so that it can raise a separate error
when the select fails, I'd like to understand the use case better.
Why would you want to make this distinction?  Requeueing the request
(as in Skip's example) doesn't make sense IMO: you set the timeout for
a reason, and that reason is that you want to give up if it takes too
long.  If you really intend to retry you're better of disabling the
timeout!

If you really want to, you can already distinguish the timeout case,
because you get an EAGAIN error then (maybe something else on Windows
-- Bernard, if you have a fix for that, please send it to me).

So a -0 unless more evidence is brought forward.

--Guido van Rossum (home page: http://www.python.org/~guido/)