From 888e9918a6ec4f8e8f289c9e788b7b2b21f89dff Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 7 Mar 2022 23:44:08 -0300 Subject: [PATCH] improve timeout handling --- lbry/extras/daemon/components.py | 2 +- lbry/torrent/tracker.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lbry/extras/daemon/components.py b/lbry/extras/daemon/components.py index f290bedf2..9312233e0 100644 --- a/lbry/extras/daemon/components.py +++ b/lbry/extras/daemon/components.py @@ -730,7 +730,7 @@ class TrackerAnnouncerComponent(Component): async def announce_forever(self): while True: - to_sleep = 60 * 10 + to_sleep = 60 * 1 for file in self.file_manager.get_filtered(): if not file.downloader: continue diff --git a/lbry/torrent/tracker.py b/lbry/torrent/tracker.py index 33cf330a4..00db99183 100644 --- a/lbry/torrent/tracker.py +++ b/lbry/torrent/tracker.py @@ -59,9 +59,10 @@ def encode(obj): class UDPTrackerClientProtocol(asyncio.DatagramProtocol): - def __init__(self): + def __init__(self, timeout = 30.0): self.transport = None self.data_queue = {} + self.timeout = timeout def connection_made(self, transport: asyncio.DatagramTransport) -> None: self.transport = transport @@ -70,7 +71,7 @@ class UDPTrackerClientProtocol(asyncio.DatagramProtocol): self.data_queue[obj.transaction_id] = asyncio.get_running_loop().create_future() self.transport.sendto(encode(obj), (tracker_ip, tracker_port)) try: - return await asyncio.wait_for(self.data_queue[obj.transaction_id], 3.0) + return await asyncio.wait_for(self.data_queue[obj.transaction_id], self.timeout) finally: self.data_queue.pop(obj.transaction_id, None) @@ -144,13 +145,17 @@ class TrackerClient: async def get_peer_list(self, info_hash, stopped=False): found = [] for done in asyncio.as_completed([self._probe_server(info_hash, *server, stopped) for server in self.servers]): - found.append(await done) + found.extend(await done) return found async def _probe_server(self, info_hash, tracker_host, tracker_port, stopped=False): - tracker_ip = await resolve_host(tracker_host, tracker_port, 'udp') - result = await self.client.announce( - info_hash, self.node_id, self.announce_port, tracker_ip, tracker_port, stopped) + try: + tracker_ip = await resolve_host(tracker_host, tracker_port, 'udp') + result = await self.client.announce( + info_hash, self.node_id, self.announce_port, tracker_ip, tracker_port, stopped) + except asyncio.TimeoutError: + log.info("Tracker timed out: %s:%d", tracker_host, tracker_port) + return [] log.info("Announced to tracker. Found %d peers for %s on %s", len(result.peers), info_hash.hex()[:8], tracker_host) self.EVENT_CONTROLLER.add((info_hash, result))