[Zodb-checkins] CVS: ZODB3/ZEO/zrpc - trigger.py:1.8

Guido van Rossum guido@python.org
Wed, 2 Oct 2002 14:37:18 -0400


Update of /cvs-repository/ZODB3/ZEO/zrpc
In directory cvs.zope.org:/tmp/cvs-serv1787/ZEO/zrpc

Modified Files:
	trigger.py 
Log Message:
Running the tests on Win98 would occasionally hang in the trigger
__init__.  The hang was in the accept() call.  I don't understand
exactly why, but I suspect it was because we were always reusing
the same port when a trigger was closed and another opened right
away.  Fixed this by cycling over ports [19950...19999].

Also cleaned up the code somewhat.


=== ZODB3/ZEO/zrpc/trigger.py 1.7 => 1.8 ===
--- ZODB3/ZEO/zrpc/trigger.py:1.7	Mon Sep 16 12:58:46 2002
+++ ZODB3/ZEO/zrpc/trigger.py	Wed Oct  2 14:37:17 2002
@@ -116,9 +116,13 @@
 
     # win32-safe version
 
+    HOST = '127.0.0.1'
+    MINPORT = 19950
+    NPORTS = 50
+
     class trigger(asyncore.dispatcher):
 
-        address = ('127.9.9.9', 19999)
+        portoffset = 0
 
         def __init__(self):
             a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -128,22 +132,23 @@
             w.setsockopt(socket.IPPROTO_TCP, 1, 1)
 
             # tricky: get a pair of connected sockets
-            host = '127.0.0.1'
-            port = 19999
-            while 1:
+            for i in range(NPORTS):
+                trigger.portoffset = (trigger.portoffset + 1) % NPORTS
+                port = MINPORT + trigger.portoffset
+                address = (HOST, port)
                 try:
-                    self.address = host, port
-                    a.bind(self.address)
+                    a.bind(address)
+                except socket.error:
+                    continue
+                else:
                     break
-                except:
-                    if port <= 19950:
-                        raise 'Bind Error', 'Cannot bind trigger!'
-                    port -= 1
+            else:
+                raise RuntimeError, 'Cannot bind trigger!'
 
             a.listen(1)
             w.setblocking(0)
             try:
-                w.connect(self.address)
+                w.connect(address)
             except:
                 pass
             r, addr = a.accept()