[Twisted-Python] [PATCH] UDP error trapping on Cygwin

Paul Marks pmarks at purdue.edu
Tue Feb 6 23:27:23 EST 2007


I wrote this program that uses Twisted with a bunch of UDP
communication, and a friend of mine got this exception while running
it on Cygwin:

Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/twisted/python/log.py", line
48, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python2.4/site-packages/twisted/python/log.py", line
33, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.4/site-packages/twisted/python/context.py",
line 59, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.4/site-packages/twisted/python/context.py",
line 37, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib/python2.4/site-packages/twisted/internet/selectreactor.py",
line 139, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "/usr/lib/python2.4/site-packages/twisted/internet/udp.py",
line 114, in doRead
    data, addr = self.socket.recvfrom(self.maxPacketSize)
socket.error: (104, 'Connection reset by peer')


My interpretation is that windows (and therefore Cygwin) can throw a
Connection Reset By Peer error when receiving UDP packets, and Twisted
only checks for the error on win32, but not on Cygwin, because Cygwin
is identified as posix.

Anyway, here's a patch I wrote that fixes the problem by checking for
ECONNRESET on every platform, although I'm not sure if that is the
correct approach.


--- udp.py      2007-01-12 14:55:11.000000000 -0500
+++ /home/paul/test/udp.py      2007-02-06 23:11:55.000000000 -0500
@@ -27,10 +27,10 @@
     from errno import WSAEINTR as EINTR
     from errno import WSAEMSGSIZE as EMSGSIZE
     from errno import WSAECONNREFUSED as ECONNREFUSED
-    from errno import WSAECONNRESET
+    from errno import WSAECONNRESET as ECONNRESET
     EAGAIN=EWOULDBLOCK
 else:
-    from errno import EWOULDBLOCK, EINTR, EMSGSIZE, ECONNREFUSED, EAGAIN
+    from errno import EWOULDBLOCK, EINTR, EMSGSIZE, ECONNREFUSED,
EAGAIN, ECONNRESET

 # Twisted Imports
 from twisted.internet import protocol, base, defer, address
@@ -116,7 +116,7 @@
                 no = se.args[0]
                 if no in (EAGAIN, EINTR, EWOULDBLOCK):
                     return
-                if (no == ECONNREFUSED) or (platformType == "win32"
and no == WSAECONNRESET):
+                if (no == ECONNREFUSED or no == ECONNRESET):
                     if self._connectedAddr:
                         self.protocol.connectionRefused()
                 else:
@@ -276,7 +276,7 @@
                 no = se.args[0]
                 if no in (EAGAIN, EINTR, EWOULDBLOCK):
                     return
-                if (no == ECONNREFUSED) or (platformType == "win32"
and no == WSAECONNRESET):
+                if (no == ECONNREFUSED or no == ECONNRESET):
                     self.protocol.connectionRefused()
                 else:
                     raise




More information about the Twisted-Python mailing list