From 86615305d7646d2e8ec1555962156d30d51bdfb0 Mon Sep 17 00:00:00 2001 From: Kevin F Date: Tue, 16 Aug 2022 19:30:05 -0500 Subject: [PATCH] Fix tor peers not being removed from connection pool when they fail --- src/gossip/client/__init__.py | 2 +- src/gossip/client/peerexchange.py | 7 ++----- src/gossip/client/streamblocks/streamfrom.py | 4 ++-- static-data/default-plugins/tor/torpeer.py | 16 ++++++++++++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/gossip/client/__init__.py b/src/gossip/client/__init__.py index 0cc74a7a..8036c1f5 100644 --- a/src/gossip/client/__init__.py +++ b/src/gossip/client/__init__.py @@ -101,7 +101,7 @@ def start_gossip_client(): # transport plugin handles the new peer add_onionr_thread( get_new_peers, - 60, 'get_new_peers', initial_sleep=120) + 120, 'get_new_peers', initial_sleep=120) # Start a new thread to stream blocks from peers # These blocks are being diffused and are stored in diff --git a/src/gossip/client/peerexchange.py b/src/gossip/client/peerexchange.py index fe59c9fc..ae526c30 100644 --- a/src/gossip/client/peerexchange.py +++ b/src/gossip/client/peerexchange.py @@ -24,9 +24,6 @@ def _do_ask_peer(peer): _ask_peer(peer) except TimeoutError: logger.debug("Timed out when asking for new peers") - except GeneralProxyError: - logger.debug("Proxy error") - logger.debug(format_exc(), terminal=True) except Exception: logger.error(format_exc(), terminal=True) @@ -49,7 +46,7 @@ def _ask_peer(peer): 'address': peer, 'callback': connectpeer.connect_peer } - logger.info("Got new peer from exchange " + peer.decode('utf-8'), terminal=True) + #logger.info("Got new peer from exchange " + peer.decode('utf-8'), terminal=True) onionrevents.event('announce_rec', data=connect_data, threaded=True) s.close() @@ -79,7 +76,7 @@ def get_new_peers(): threads = [] for peer in peers_we_ask: t = Thread( - target=_do_ask_peer, + target=_do_ask_peer, args=[peer], daemon=True, name='_do_ask_peer') t.start() threads.append(t) diff --git a/src/gossip/client/streamblocks/streamfrom.py b/src/gossip/client/streamblocks/streamfrom.py index 1bbdfd0d..1cd18ec0 100644 --- a/src/gossip/client/streamblocks/streamfrom.py +++ b/src/gossip/client/streamblocks/streamfrom.py @@ -67,7 +67,7 @@ def stream_from_peers(): stream_times = 100 try: sock = peer.get_socket(CONNECT_TIMEOUT) - except ConnectionRefusedError: + except (TimeoutError, ConnectionRefusedError) as _: need_socket_lock.release() return except Exception: @@ -118,7 +118,7 @@ def stream_from_peers(): raise # Tell them to keep streaming sock.sendall(int(1).to_bytes(1, 'big')) - except (BrokenPipeError, TimeoutError) as e: + except (BrokenPipeError, TimeoutError, ConnectionError) as e: pass #logger.debug(f"{e} when streaming from peers", terminal=True) #logger.debug(traceback.format_exc()) diff --git a/static-data/default-plugins/tor/torpeer.py b/static-data/default-plugins/tor/torpeer.py index 87bd4630..048bca54 100644 --- a/static-data/default-plugins/tor/torpeer.py +++ b/static-data/default-plugins/tor/torpeer.py @@ -3,12 +3,24 @@ import socks from gossip.peerset import gossip_peer_set import logger + class HandleRevc: def __init__(self, sock): self.sock_recv = sock.recv + self.sock = sock def recv(self, *args, **kwargs): - return self.sock_recv(*args, **kwargs) + try: + got_data = self.sock_recv(*args, **kwargs) + if not len(got_data): + raise ConnectionError("Peer socket returned empty value") + return got_data + except Exception: + try: + gossip_peer_set.remove(self.sock) + except KeyError: + pass + raise class TorPeer: @@ -37,7 +49,7 @@ class TorPeer: pass else: logger.debug(f"Could not create socket to peer {self.transport_address}", terminal=True) - raise + raise TimeoutError mock_recv = HandleRevc(s) s.recv = mock_recv.recv return s