[or-cvs] r18654: {torctl} Handle some stream failure conditions properly, and fix an i (torctl/trunk/python/TorCtl)

mikeperry at seul.org mikeperry at seul.org
Fri Feb 20 10:21:02 UTC 2009


Author: mikeperry
Date: 2009-02-20 05:21:02 -0500 (Fri, 20 Feb 2009)
New Revision: 18654

Modified:
   torctl/trunk/python/TorCtl/PathSupport.py
   torctl/trunk/python/TorCtl/StatsSupport.py
   torctl/trunk/python/TorCtl/TorCtl.py
Log:

Handle some stream failure conditions properly, and fix an
issue with receiving NEWDESCs.

Also more cleanly alert the user when the Tor control port
disappears.



Modified: torctl/trunk/python/TorCtl/PathSupport.py
===================================================================
--- torctl/trunk/python/TorCtl/PathSupport.py	2009-02-20 08:04:38 UTC (rev 18653)
+++ torctl/trunk/python/TorCtl/PathSupport.py	2009-02-20 10:21:02 UTC (rev 18654)
@@ -1186,7 +1186,13 @@
           # FIXME: How come some routers are non-existant? Shouldn't
           # we have gotten an NS event to notify us they
           # disappeared?
-          plog("NOTICE", "Error building circ: "+str(e.args))
+          plog("WARN", "Error building circ: "+str(e.args))
+          self.last_exit = None
+          # Kill this stream
+          plog("NOTICE", "Closing stream "+str(stream.strm_id))
+          # END_STREAM_REASON_DESTROY
+          self.c.close_stream(stream.strm_id, "5") 
+          return
       for u in unattached_streams:
         plog("DEBUG",
            "Attaching "+str(u.strm_id)+" pending build of "+str(circ.circ_id))
@@ -1404,7 +1410,7 @@
       except TorCtl.ErrorReply, e:
         # FIXME: How come some routers are non-existant? Shouldn't
         # we have gotten an NS event to notify us they disappeared?
-        plog("NOTICE", "Error building circuit: " + str(e.args))
+        plog("WARN", "Error building circuit: " + str(e.args))
 
   def circ_status_event(self, c):
     """ Handle circuit status events """

Modified: torctl/trunk/python/TorCtl/StatsSupport.py
===================================================================
--- torctl/trunk/python/TorCtl/StatsSupport.py	2009-02-20 08:04:38 UTC (rev 18653)
+++ torctl/trunk/python/TorCtl/StatsSupport.py	2009-02-20 10:21:02 UTC (rev 18654)
@@ -627,12 +627,14 @@
       if s.status not in ("NEW", "NEWRESOLVE", "REMAP"):
         circ = self.streams[s.strm_id].circ
         if not circ: circ = self.streams[s.strm_id].pending_circ
+        # XXX: Figure out who to attribute this failure to
         if circ and circ.circ_id != s.circ_id:
           plog("WARN", str(s.strm_id) + " has mismatch of "
                 +str(s.circ_id)+" v "+str(circ.circ_id))
         if s.circ_id and s.circ_id not in self.circuits:
           plog("NOTICE", "Unknown circuit "+str(s.circ_id)
                 +" for stream "+str(s.strm_id))
+          PathBuilder.stream_status_event(self, s)
           return
       
       if s.status == "DETACHED":

Modified: torctl/trunk/python/TorCtl/TorCtl.py
===================================================================
--- torctl/trunk/python/TorCtl/TorCtl.py	2009-02-20 08:04:38 UTC (rev 18653)
+++ torctl/trunk/python/TorCtl/TorCtl.py	2009-02-20 10:21:02 UTC (rev 18654)
@@ -426,6 +426,9 @@
     while 1:
       try:
         isEvent, reply = self._read_reply()
+      except TorCtlClosed:
+        plog("NOTICE", "Tor closed control connection. Exiting event thread.")
+        return
       except:
         if not self._closed:
           self._err(sys.exc_info())
@@ -491,7 +494,7 @@
 
   def _sendImpl(self, sendFn, msg):
     """DOCDOC"""
-    if self._thread is None:
+    if self._thread is None and not self._closed:
       self.launch_thread(1)
     # This condition will get notified when we've got a result...
     condition = threading.Condition()
@@ -549,7 +552,11 @@
   def _read_reply(self):
     lines = []
     while 1:
-      line = self._s.readline().strip()
+      line = self._s.readline()
+      if not line:
+        self._closed = True
+        raise TorCtlClosed() 
+      line = line.strip()
       if self._debugFile:
         self._debugFile.write("  %s\n" % line)
       if len(line)<4:
@@ -588,7 +595,7 @@
       lines = amsg.split("\n")
       if len(lines) > 2:
         amsg = "\n".join(lines[:2]) + "\n"
-      self._debugFile.write(">>> %s" % amsg)
+      self._debugFile.write(str(time.time())+">>> "+amsg)
     self._s.write(msg)
 
   def sendAndRecv(self, msg="", expectedTypes=("250", "251")):
@@ -1141,6 +1148,7 @@
       elif len(r) != 1:
         plog("WARN", "Multiple descs for "+i+" after NEWDESC")
       r = r[0]
+      ns = ns[0]
       if r and r.idhex in self.consensus:
         if ns.orhash != self.consensus[r.idhex].orhash:
           plog("WARN", "Getinfo and consensus disagree for "+r.idhex)



More information about the tor-commits mailing list