[or-cvs] Pass exceptions back to the client thread from TorCtl1. Geo...

Nick Mathewson nickm at seul.org
Wed Nov 16 19:23:00 UTC 2005


Update of /home/or/cvsroot/control/python
In directory moria:/tmp/cvs-serv489

Modified Files:
	TorCtl1.py 
Log Message:
Pass exceptions back to the client thread from TorCtl1. Geoff -- if this works for you, let me know so I can port it to TorCtl0.

Index: TorCtl1.py
===================================================================
RCS file: /home/or/cvsroot/control/python/TorCtl1.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- TorCtl1.py	15 Nov 2005 20:37:42 -0000	1.11
+++ TorCtl1.py	16 Nov 2005 19:22:57 -0000	1.12
@@ -121,6 +121,8 @@
         self._sendLock = threading.RLock()
         self._queue = Queue.Queue()
         self._thread = None
+        self._closedEx = None
+        self._closed = 0
 
     def debug(self, f):
         """DOCDOC"""
@@ -147,6 +149,7 @@
         try:
             self._queue.put("CLOSE")
             self._s.close()
+            self._closed = 1
         finally:
             self._sendLock.release()
 
@@ -155,11 +158,25 @@
            as events or as responses to other commands.
         """
         while 1:
+            ex = None
             try:
                 lines = _read_reply(self._s,self._debugFile)
-            except (OSError, socket.error), _:
-                if self._queue.get(timeout=0) != "CLOSE":
-                    raise
+            except (OSError, socket.error), err:
+                if self._queue.get(timeout=0) == "CLOSE":
+                    ex = TorCtl.TorCtlClosed()
+                else:
+                    ex = err
+
+            if ex is not None:
+                self._sendLock.acquire()
+                try:
+                    self._closedEx = ex
+                    self._closed = 1
+                finally:
+                    self._sendLock.release()
+                cb = self._queue.get()
+                cb("EXCEPTION")
+
             assert lines
             if lines[0][0][0] == "6":
                 if self._handler is not None:
@@ -181,6 +198,11 @@
         # Here's where the result goes...
         result = []
 
+        if self._closedEx is not None:
+            raise self._closedEx
+        elif self._closed:
+            raise TorCtl.TorCtlClosed()
+
         def cb(lines,condition=condition,result=result):
             condition.acquire()
             try:
@@ -215,6 +237,8 @@
         # ...And handle the answer appropriately.
         assert len(result) == 1
         lines = result[0]
+        if lines == "EXCEPTION":
+            raise self._closedEx
         for tp, msg, _ in lines:
             if tp[0] in '45':
                 raise TorCtl.ErrorReply("%s %s"%(tp, msg))



More information about the tor-commits mailing list