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

[tor-commits] [flashproxy/master] Capture registration helper stdout, stderr, and exit status.



commit 89e54229a9e94c7cfb385d42d195f65a885a50ff
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Sun Sep 23 17:28:15 2012 -0700

    Capture registration helper stdout, stderr, and exit status.
    
    The registration helpers run in a thread, waiting in an infinite loop
    until the main process asks for a registration. This is necessary now
    because flashproxy-client needs to register multiple times in order to
    keep getting service. When #5426 is done, it should only be necessary to
    register once in the common case, and the thread won't be necessary.
---
 flashproxy-client |   39 ++++++++++++++++++++++++++++++++++-----
 1 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/flashproxy-client b/flashproxy-client
index f0491dd..746f5eb 100755
--- a/flashproxy-client
+++ b/flashproxy-client
@@ -13,6 +13,7 @@ import socket
 import struct
 import subprocess
 import sys
+import threading
 import time
 import traceback
 import urllib
@@ -91,9 +92,14 @@ def safe_str(s):
     else:
         return s
 
+log_lock = threading.Lock()
 def log(msg):
-    print >> options.log_file, (u"%s %s" % (time.strftime(LOG_DATE_FORMAT), msg)).encode("UTF-8")
-    options.log_file.flush()
+    log_lock.acquire()
+    try:
+        print >> options.log_file, (u"%s %s" % (time.strftime(LOG_DATE_FORMAT), msg)).encode("UTF-8")
+        options.log_file.flush()
+    finally:
+        log_lock.release()
 
 def parse_addr_spec(spec, defhost = None, defport = None):
     host = None
@@ -639,10 +645,15 @@ def report_pending():
     log(u"locals  (%d): %s" % (len(locals), [safe_format_peername(x) for x in locals]))
     log(u"remotes (%d): %s" % (len(remotes), [safe_format_peername(x) for x in remotes]))
  
+register_condvar = threading.Condition()
 def register():
     if not options.register:
         return
+    register_condvar.acquire()
+    register_condvar.notify()
+    register_condvar.release()
 
+def register_one():
     # sys.path[0] is initialized to the directory containing the Python script file.
     script_dir = sys.path[0]
     if not script_dir:
@@ -655,9 +666,23 @@ def register():
         command += ["-f", options.facilitator_url]
     command += [spec]
     try:
-        p = subprocess.Popen(command)
+        p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        stdout, stderr = p.communicate()
     except OSError, e:
-        log(u"Failed to register: %s" % str(e))
+        log(u"Error running registration helper: %s" % str(e))
+    for line in stdout.splitlines():
+        log(line)
+    for line in stderr.splitlines():
+        log(line)
+    if p.returncode != 0:
+        log("Registration helper ended with status %d." % p.returncode)
+
+def registration_thread_func():
+    register_condvar.acquire()
+    while True:
+        register_condvar.wait()
+        register_one()
+    register_condvar.release()
 
 def proxy_chunk_local_to_remote(local, remote, data = None):
     if data is None:
@@ -955,7 +980,11 @@ if __name__ == "__main__":
     # Locals not yet linked with a remote. This is a subset of remotes.
     unlinked_locals = []
 
-    register()
+    if options.register:
+        registration_thread = threading.Thread(target=registration_thread_func, name="register")
+        registration_thread.daemon = True
+        registration_thread.start()
+        register()
 
     if options.daemonize:
         log(u"Daemonizing.")

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