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

[tor-commits] [stem/master] Making the tor launching timeout an arg



commit 8293c518d02f879110d45e0cff074e7d31d2fbfb
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Oct 21 07:28:27 2011 -0700

    Making the tor launching timeout an arg
    
    Moving the timeout for launching a tor process from being a constant to being
    an argument of the function.
---
 stem/process.py     |   27 +++++++++++++++++----------
 stem/util/system.py |    4 ++--
 2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/stem/process.py b/stem/process.py
index f049610..75e8f6f 100644
--- a/stem/process.py
+++ b/stem/process.py
@@ -8,17 +8,24 @@ import signal
 import subprocess
 
 # number of seconds before we time out our attempt to start a tor instance
-TOR_INIT_TIMEOUT = 90
+DEFAULT_INIT_TIMEOUT = 90
 
-def launch_tor(torrc_path, init_msg_handler = None):
+def launch_tor(torrc_path, init_msg_handler = None, timeout = DEFAULT_INIT_TIMEOUT):
   """
   Initializes a tor process. This blocks until initialization completes or we
   error out.
   
+  If tor's data directory is missing or stale then bootstrapping will include
+  making several requests to the directory authorities which can take a little
+  while. Usually this is done in 50 seconds or so, but occasionally calls seem
+  to get stuck, taking well over the default timeout.
+  
   Arguments:
     torrc_path (str)           - location of the torrc for us to use
     init_msg_handler (functor) - optional functor that will be provided with
                                  tor's initialization stdout as we get it
+    timeout (int)              - time after which the attempt to start tor is
+                                 aborted, no timeouts are applied if None
   
   Returns:
     subprocess.Popen instance for the tor subprocess
@@ -35,14 +42,14 @@ def launch_tor(torrc_path, init_msg_handler = None):
   # starts a tor subprocess, raising an OSError if it fails
   tor_process = subprocess.Popen(["tor", "-f", torrc_path], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
   
-  # time ourselves out if we reach TOR_INIT_TIMEOUT
-  def timeout_handler(signum, frame):
-    # terminates the uninitialized tor process and raise on timeout
-    tor_process.kill()
-    raise OSError("reached a %i second timeout without success" % TOR_INIT_TIMEOUT)
-  
-  signal.signal(signal.SIGALRM, timeout_handler)
-  signal.alarm(TOR_INIT_TIMEOUT)
+  if timeout:
+    def timeout_handler(signum, frame):
+      # terminates the uninitialized tor process and raise on timeout
+      tor_process.kill()
+      raise OSError("reached a %i second timeout without success" % timeout)
+    
+    signal.signal(signal.SIGALRM, timeout_handler)
+    signal.alarm(timeout)
   
   while True:
     init_line = tor_process.stdout.readline().strip()
diff --git a/stem/util/system.py b/stem/util/system.py
index 5f7e9ca..a27c265 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -272,7 +272,7 @@ def call(command, suppress_exc = True):
   """
   Issues a command in a subprocess, blocking until completion and returning the
   results. This is not actually ran in a shell so pipes and other shell syntax
-  aren't permitted.
+  are not permitted.
   
   Arguments:
     command (str)       - command to be issued
@@ -280,7 +280,7 @@ def call(command, suppress_exc = True):
                           this raises the exception
   
   Returns:
-    List with the lines of output from the command, None in case of failure if
+    list with the lines of output from the command, None in case of failure if
     suppress_exc is True
   
   Raises:



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