[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] * Need to fix the Deferred class decorator timeout.



commit 365bd5a1a631191808a110ded51f520806ab342e
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date:   Fri Sep 28 05:37:49 2012 +0000

    * Need to fix the Deferred class decorator timeout.
---
 ooni/plugins/bridget.py |   20 +++++++++++---------
 ooni/utils/timer.py     |   31 +++++++++++++++++++++----------
 2 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/ooni/plugins/bridget.py b/ooni/plugins/bridget.py
index 208b4c3..538e2f3 100644
--- a/ooni/plugins/bridget.py
+++ b/ooni/plugins/bridget.py
@@ -282,6 +282,7 @@ class BridgetTest(OONITest):
             from ooni.utils.onion   import start_tor, remove_public_relays
             from ooni.utils.onion   import setup_done, setup_fail
             from ooni.utils.onion   import CustomCircuit
+            from ooni.utils.timer   import timeout
             from ooni.lib.txtorcon  import TorConfig, TorState
         except ImportError:
             raise TxtorconImportError
@@ -365,19 +366,19 @@ class BridgetTest(OONITest):
             log.err("Attaching custom circuit builder failed: %s" % state)
 
 
-        log.msg("Bridget: initiating test ... ") ## Start the experiment
-        all_of_the_bridges = self.bridges['all']
-        all_of_the_relays  = self.relays['all']  ## Local copy of orginal lists
+        log.msg("Bridget: initiating test ... ")  ## Start the experiment
 
-        if self.bridges['remaining']() >= 1 and not 'Bridge' in self.config.config:
+        if self.bridges['remaining']() >= 1 \
+                and not 'Bridge' in self.config.config:
             self.bridges['current'] = self.bridges['all'][0]
             self.config.Bridge = self.bridges['current']
-                                                 ## avoid starting several
-            self.config.save()                   ## processes 
+                                                  ## avoid starting several
+            self.config.save()                    ## processes 
             assert self.config.config.has_key('Bridge'), "NO BRIDGE"
 
-            state = start_tor(reactor, self.config, self.control_port, 
-                              self.tor_binary, self.data_directory)
+            state = timeout(self.circuit_timeout)(start_tor(
+                    reactor, self.config, self.control_port, 
+                    self.tor_binary, self.data_directory))
             state.addCallbacks(setup_done, setup_fail)
             state.addCallback(remove_public_relays, self.bridges)
 
@@ -396,7 +397,8 @@ class BridgetTest(OONITest):
             all = []
             for bridge in self.bridges['all'][1:]:
                 self.bridges['current'] = bridge
-                new = defer.Deferred()
+                #new = defer.Deferred()
+                new = defer.waitForDeferred(state)
                 new.addCallback(reconfigure_bridge, state, self.bridges)
                 all.append(new)
 
diff --git a/ooni/utils/timer.py b/ooni/utils/timer.py
index 8ad9b79..24074f9 100644
--- a/ooni/utils/timer.py
+++ b/ooni/utils/timer.py
@@ -9,28 +9,39 @@
 # :copyright: copyright (c) 2012, Isis Lovecruft, The Tor Project Inc.
 # 
 
-
 class TimeoutError(Exception):
     """Raised when a timer runs out."""
     pass
 
-def timeout(secs, e=None):
+def timeout(seconds, e=None):
     """
-    A decorator for blocking methods to cause them to timeout.
+    A decorator for blocking methods to cause them to timeout. Can be used
+    like this: 
+        @timeout(30)
+        def foo():
+            for x in xrange(1000000000):
+                print x
+    or like this:
+        ridiculous = timeout(30)(foo)
+
+    :param seconds:
+        Number of seconds to wait before raising :class:`TimeoutError`.
+    :param e:
+        Error message to pass to :class:`TimeoutError`. Default None.
     """
-    import signal
-    import functools.wraps
+    from signal    import alarm, signal, SIGALRM
+    from functools import wraps
+
     def decorator(func):
         def _timeout(signum, frame):
             raise TimeoutError, e
-
         def wrapper(*args, **kwargs):
-            signal.signal(signal.SIGALRM, _timeout)
-            signal.alarm(secs)
+            signal(SIGALRM, _timeout)
+            alarm(seconds)
             try:
                 res = func(*args, **kwargs)
             finally:
-                signal.alarm(0)
+                alarm(0)
             return res
-        return functools.wraps(func)(wrapper)
+        return wraps(func)(wrapper)
     return decorator



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits