[tor-commits] [sbws/master] fix: !minor. Catch SocketClosed when stopping

juga at torproject.org juga at torproject.org
Tue Mar 5 18:46:14 UTC 2019


commit 315215dc66089302a92bd3a01c37d008f698811f
Author: juga0 <juga at riseup.net>
Date:   Tue Mar 5 04:26:42 2019 +0000

    fix: !minor. Catch SocketClosed when stopping
    
    When SocketClosed is raised and the scanner is stopping, catch the
    exception.
    In #28869 similar exceptions were catched, but this was forgotten.
    
    Bugfix v0.6.0.
---
 sbws/core/scanner.py       | 13 ++++++++++---
 sbws/lib/circuitbuilder.py |  2 +-
 sbws/util/requests.py      |  3 +++
 sbws/util/stem.py          | 14 ++++++++++++--
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/sbws/core/scanner.py b/sbws/core/scanner.py
index 4ad2cb5..cdf0b9b 100644
--- a/sbws/core/scanner.py
+++ b/sbws/core/scanner.py
@@ -240,6 +240,9 @@ def measure_relay(args, conf, destinations, cb, rl, relay):
     log.debug('Measuring %s %s', relay.nickname, relay.fingerprint)
     s = requests_utils.make_session(
         cb.controller, conf.getfloat('general', 'http_timeout'))
+    # Probably because the scanner is stopping.
+    if s is None:
+        return None
     # Pick a destionation
     dest = destinations.next()
     # If there is no any destination at this point, it can not continue.
@@ -401,13 +404,17 @@ def result_putter_error(target):
     measurement -- and return that function so it can be used by someone else
     '''
     def closure(object):
+        if settings.end_event.is_set():
+            return
         # The only object that can be here if there is not any uncatched
         # exception is stem.SocketClosed when stopping sbws
         # An exception here means that the worker thread finished.
         log.warning(FILLUP_TICKET_MSG)
-        # To print the traceback that happened in the thread, not here in the
-        # main process
-        traceback.print_exception(type(object), object, object.__traceback__)
+        # To print the traceback that happened in the thread, not here in
+        # the main process.
+        log.warning("".join(traceback.format_exception(
+            type(object), object, object.__traceback__))
+            )
     return closure
 
 
diff --git a/sbws/lib/circuitbuilder.py b/sbws/lib/circuitbuilder.py
index c5bae5c..45b807f 100644
--- a/sbws/lib/circuitbuilder.py
+++ b/sbws/lib/circuitbuilder.py
@@ -66,7 +66,7 @@ class CircuitBuilder:
             circ_id = c.new_circuit(
                 path, await_build=True, timeout=timeout)
         except (InvalidRequest, CircuitExtensionFailed,
-                ProtocolError, Timeout) as e:
+                ProtocolError, Timeout, SocketClosed) as e:
             return None, str(e)
         return circ_id, None
 
diff --git a/sbws/util/requests.py b/sbws/util/requests.py
index 615916d..ede847d 100644
--- a/sbws/util/requests.py
+++ b/sbws/util/requests.py
@@ -24,6 +24,9 @@ def make_session(controller, timeout):
     """
     s = TimedSession()
     socks_info = stem_utils.get_socks_info(controller)
+    # Probably because scanner is stopping.
+    if socks_info is None:
+        return None
     s.proxies = {
         'http': 'socks5h://{}:{}'.format(*socks_info),
         'https': 'socks5h://{}:{}'.format(*socks_info),
diff --git a/sbws/util/stem.py b/sbws/util/stem.py
index cd8ca6e..05f8e90 100644
--- a/sbws/util/stem.py
+++ b/sbws/util/stem.py
@@ -14,6 +14,7 @@ import os
 from sbws.globals import fail_hard
 from sbws.globals import (TORRC_STARTING_POINT, TORRC_RUNTIME_OPTIONS,
                           TORRC_OPTIONS_CAN_FAIL)
+from sbws import settings
 
 log = logging.getLogger(__name__)
 stream_building_lock = RLock()
@@ -50,6 +51,11 @@ def add_event_listener(controller, func, event):
 def remove_event_listener(controller, func):
     try:
         controller.remove_event_listener(func)
+    except SocketClosed as e:
+        if not settings.end_event.is_set():
+            log.debug(e)
+        else:
+            log.exception(e)
     except ProtocolError as e:
         log.exception("Exception trying to remove event %s", e)
 
@@ -245,9 +251,13 @@ def get_socks_info(controller):
     try:
         socks_ports = controller.get_listeners(Listener.SOCKS)
         return socks_ports[0]
+    except SocketClosed as e:
+        if not settings.end_event.is_set():
+            log.debug(e)
+    # This might need to return the eception if this happen in other cases
+    # than when stopping the scanner.
     except ControllerError as e:
-        log.exception("Exception trying to get socks info: %e.", e)
-        exit(1)
+        log.debug(e)
 
 
 def only_relays_with_bandwidth(controller, relays, min_bw=None, max_bw=None):





More information about the tor-commits mailing list