[tor-commits] [stem/master] Moving launch_tor() cleanup to a finally block
atagar at torproject.org
atagar at torproject.org
Thu May 29 16:05:03 UTC 2014
commit b7a04768747681bc7889ef0b9a959b918557b3d9
Author: Damian Johnson <atagar at torproject.org>
Date: Thu May 29 08:15:42 2014 -0700
Moving launch_tor() cleanup to a finally block
Whenever launch_tor() finishes, either with success or failure we want to do
two things:
* stop our alarm if we were running with a timeout
* clean up our temporary torrc if we made a blank one
We did this in an ad-hoc fashion, but it better belongs in a finally block.
---
stem/process.py | 81 +++++++++++++++++++++++++------------------------------
1 file changed, 36 insertions(+), 45 deletions(-)
diff --git a/stem/process.py b/stem/process.py
index 1f5461d..ad05659 100644
--- a/stem/process.py
+++ b/stem/process.py
@@ -107,14 +107,8 @@ def launch_tor(tor_cmd = 'tor', args = None, torrc_path = None, completion_perce
if timeout:
def timeout_handler(signum, frame):
# terminates the uninitialized tor process and raise on timeout
- if temp_file:
- try:
- os.remove(temp_file)
- except:
- pass
tor_process.kill()
-
raise OSError('reached a %i second timeout without success' % timeout)
signal.signal(signal.SIGALRM, timeout_handler)
@@ -124,55 +118,52 @@ def launch_tor(tor_cmd = 'tor', args = None, torrc_path = None, completion_perce
problem_line = re.compile('\[(warn|err)\] (.*)$')
last_problem = 'Timed out'
- while True:
- # Tor's stdout will be read as ASCII bytes. This is fine for python 2, but
- # in python 3 that means it'll mismatch with other operations (for instance
- # the bootstrap_line.search() call later will fail).
- #
- # It seems like python 2.x is perfectly happy for this to be unicode, so
- # normalizing to that.
-
- init_line = tor_process.stdout.readline().decode('utf-8', 'replace').strip()
-
- # this will provide empty results if the process is terminated
- if not init_line:
- if timeout:
- signal.alarm(0) # stop alarm
-
- # ... but best make sure
+ try:
+ while True:
+ # Tor's stdout will be read as ASCII bytes. This is fine for python 2, but
+ # in python 3 that means it'll mismatch with other operations (for instance
+ # the bootstrap_line.search() call later will fail).
+ #
+ # It seems like python 2.x is perfectly happy for this to be unicode, so
+ # normalizing to that.
- tor_process.kill()
+ init_line = tor_process.stdout.readline().decode('utf-8', 'replace').strip()
- raise OSError('Process terminated: %s' % last_problem)
+ # this will provide empty results if the process is terminated
- # provide the caller with the initialization message if they want it
+ if not init_line:
+ tor_process.kill() # ... but best make sure
+ raise OSError('Process terminated: %s' % last_problem)
- if init_msg_handler:
- init_msg_handler(init_line)
+ # provide the caller with the initialization message if they want it
- # return the process if we're done with bootstrapping
- bootstrap_match = bootstrap_line.search(init_line)
- problem_match = problem_line.search(init_line)
+ if init_msg_handler:
+ init_msg_handler(init_line)
- if bootstrap_match and int(bootstrap_match.groups()[0]) >= completion_percent:
- if timeout:
- signal.alarm(0) # stop alarm
+ # return the process if we're done with bootstrapping
- if temp_file:
- try:
- os.remove(temp_file)
- except:
- pass
+ bootstrap_match = bootstrap_line.search(init_line)
+ problem_match = problem_line.search(init_line)
- return tor_process
- elif problem_match:
- runlevel, msg = problem_match.groups()
+ if bootstrap_match and int(bootstrap_match.group(1)) >= completion_percent:
+ return tor_process
+ elif problem_match:
+ runlevel, msg = problem_match.groups()
- if not 'see warnings above' in msg:
- if ': ' in msg:
- msg = msg.split(': ')[-1].strip()
+ if not 'see warnings above' in msg:
+ if ': ' in msg:
+ msg = msg.split(': ')[-1].strip()
- last_problem = msg
+ last_problem = msg
+ finally:
+ if timeout:
+ signal.alarm(0) # stop alarm
+
+ if temp_file:
+ try:
+ os.remove(temp_file)
+ except:
+ pass
def launch_tor_with_config(config, tor_cmd = 'tor', completion_percent = 100, init_msg_handler = None, timeout = DEFAULT_INIT_TIMEOUT, take_ownership = False):
More information about the tor-commits
mailing list