[issue8831] recv and recvfrom on UDP socket do not return/throw exception after a close()

STINNER Victor report at bugs.python.org
Thu May 27 13:35:59 CEST 2010


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

> This is in contrast with the standard C++/C behavior, where a close()
> on a socket causes an asynchronous and immediate exception/return with
> error on the functions that are using the socket at the same time (but
> in another thread).

Are you sure of that? I don't see how Python behaviour would be different to a the same program written in C. Could you write a short example written in C to prove that?

--

To avoid this issue (Python blocks on recv() whereas a thread closed the socket), you should check that there is data on the socket before reading the data. Eg. TCPServer.serve_forever() uses select.select() to avoid this issue. Extract:

   while not self.__shutdown_request:
       # XXX: Consider using another file descriptor or
       # connecting to the socket to wake this up instead of
       # polling. Polling reduces our responsiveness to a
       # shutdown request and wastes cpu at all other times.
       r, w, e = select.select([self], [], [], poll_interval)
       if self in r:
           self._handle_request_noblock()

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8831>
_______________________________________


More information about the Python-bugs-list mailing list