[Twisted-Python] Handling exceptions in twisted.internet.udp?

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Mon Feb 8 11:57:05 EST 2010

On 04:20 pm, bra at fsn.hu wrote:
>I have a code, which opens persistent (connected, see:
>UDP connections, and sends and receives data on them.
>The problem is that occasionally I receive ICMP 13 (administratively
>prohibited) errors from the destinations, which triggers and exception
>and closes the UDP socket. I would like to keep that open, no matter
>what happens.
>On FreeBSD the exception comes during writing into the socket with
>transport.write, so this is an easy one.

This is probably a bug:


So you're right that you have to handle it in your app code (and that 
it's easy to do so, fortunately).  Hopefully someday this will no longer 
be true.
>But on Linux it's a little bit harder, because there I get exception 
>during writing, but reading, which is handled here:

And this looks like another bug. :)
>line 109, in doRead
>            data, addr = self.socket.recvfrom(self.maxPacketSize)
>        socket.error: [Errno 113] No route to host
>I wonder what is the preferred way of achieving that the socket will be
>write()-able and kept open regardless what ICMP or other sending or
>receiving error occurs?

It seems like udp.py should be handling EHOSTUNREACH (and probably 
several more errors that it also misses currently).  There's already a 
`connectionRefused` callback which could be invoked to handle these 

This seems pretty related to another open ticket:


Aside from monkey-patching the doRead method, I don't see a way for an 
application to handle this one.

Development has started on fixing each of these issues.  The latter 
seems to even have been reviewed and is now waiting for someone to 
address the review feedback.


More information about the Twisted-Python mailing list