Skip to content
This repository has been archived by the owner on May 4, 2021. It is now read-only.

Commit

Permalink
fix: !minor. Catch SocketClosed when stopping
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
juga0 committed Mar 5, 2019
1 parent ca72016 commit 315215d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
13 changes: 10 additions & 3 deletions sbws/core/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion sbws/lib/circuitbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _build_circuit_impl(self, path):
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

Expand Down
3 changes: 3 additions & 0 deletions sbws/util/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 12 additions & 2 deletions sbws/util/stem.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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()
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 315215d

Please sign in to comment.