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

Attila Nagy bra at fsn.hu
Mon Feb 8 09:20:31 MST 2010


Hello,

I have a code, which opens persistent (connected, see: 
http://twistedmatrix.com/documents/current/core/howto/udp.html#auto2) 
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.
But on Linux it's a little bit harder, because there I get exception not 
during writing, but reading, which is handled here:
http://twistedmatrix.com/trac/browser/tags/releases/twisted-9.0.0/twisted/internet/udp.py#L113

The exception looks like this:
2010-02-08 12:09:15+0100 [-] reactor class: 
twisted.internet.selectreactor.SelectReactor.
2010-02-08 12:10:22+0100 [__builtin__.connectedUDP (UDP)] Unhandled Error
        Traceback (most recent call last):
          File 
"/usr/lib64/python2.6/site-packages/Twisted-9.0.0_r27775-py2.6-linux-x86_64.egg/twisted/python/log.py", 
line 84, in callWithLogger
            return callWithContext({"system": lp}, func, *args, **kw)
          File 
"/usr/lib64/python2.6/site-packages/Twisted-9.0.0_r27775-py2.6-linux-x86_64.egg/twisted/python/log.py", 
line 69, in callWithContext
            return context.call({ILogContext: newCtx}, func, *args, **kw)
          File 
"/usr/lib64/python2.6/site-packages/Twisted-9.0.0_r27775-py2.6-linux-x86_64.egg/twisted/python/context.py", 
line 59, in callWithContext
            return self.currentContext().callWithContext(ctx, func, 
*args, **kw)
          File 
"/usr/lib64/python2.6/site-packages/Twisted-9.0.0_r27775-py2.6-linux-x86_64.egg/twisted/python/context.py", 
line 37, in callWithContext
            return func(*args,**kw)
        --- <exception caught here> ---
          File 
"/usr/lib64/python2.6/site-packages/Twisted-9.0.0_r27775-py2.6-linux-x86_64.egg/twisted/internet/selectreactor.py", 
line 146, in _doReadOrWrite
            why = getattr(selectable, method)()
          File 
"/usr/lib64/python2.6/site-packages/Twisted-9.0.0_r27775-py2.6-linux-x86_64.egg/twisted/internet/udp.py", 
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?

Thanks,





More information about the Twisted-Python mailing list