diff --git a/electrum/interface.py b/electrum/interface.py index 1ac52d0be..4630dd902 100644 --- a/electrum/interface.py +++ b/electrum/interface.py @@ -53,8 +53,9 @@ class NotificationSession(ClientSession): self.scripthash = scripthash self.header = header - @aiosafe async def handle_request(self, request): + # note: if server sends malformed request and we raise, the superclass + # will catch the exception, count errors, and at some point disconnect if isinstance(request, Notification): if request.method == 'blockchain.scripthash.subscribe' and self.scripthash is not None: scripthash, status = request.args @@ -65,6 +66,13 @@ class NotificationSession(ClientSession): else: assert False, request.method + async def send_request(self, *args, timeout=-1, **kwargs): + if timeout == -1: + timeout = 20 if not self.proxy else 30 + return await asyncio.wait_for( + super().send_request(*args, **kwargs), + timeout) + # FIXME this is often raised inside a TaskGroup, but then it's not silent :( class GracefulDisconnect(AIOSafeSilentException): pass @@ -262,7 +270,7 @@ class Interface(PrintError): return None async def get_block_header(self, height, assert_mode): - res = await asyncio.wait_for(self.session.send_request('blockchain.block.header', [height]), 5) + res = await self.session.send_request('blockchain.block.header', [height], timeout=5) return blockchain.deserialize_header(bytes.fromhex(res), height) async def request_chunk(self, idx, tip): @@ -304,7 +312,7 @@ class Interface(PrintError): self.tip = new_header['block_height'] await copy_header_queue.put(new_header) except concurrent.futures.TimeoutError: - await asyncio.wait_for(self.session.send_request('server.ping'), 10) + await self.session.send_request('server.ping', timeout=10) def close(self): self.fut.cancel() diff --git a/electrum/network.py b/electrum/network.py index b9e12752f..e6f77d614 100644 --- a/electrum/network.py +++ b/electrum/network.py @@ -695,7 +695,7 @@ class Network(PrintError): size = max(size, 0) try: self.requested_chunks.add(index) - res = await asyncio.wait_for(session.send_request('blockchain.block.headers', [index * 2016, size]), 20) + res = await session.send_request('blockchain.block.headers', [index * 2016, size]) finally: try: self.requested_chunks.remove(index) except KeyError: pass @@ -816,4 +816,4 @@ class Network(PrintError): await asyncio.sleep(0.1) async def attempt_fee_estimate_update(self): - await asyncio.wait_for(self.request_fee_estimates(self.interface), 5) + await self.request_fee_estimates(self.interface) diff --git a/electrum/synchronizer.py b/electrum/synchronizer.py index a939f77e3..ba4611d64 100644 --- a/electrum/synchronizer.py +++ b/electrum/synchronizer.py @@ -120,7 +120,7 @@ class Synchronizer(PrintError): await group.spawn(self.get_transaction, tx_hash) async def get_transaction(self, tx_hash): - result = await asyncio.wait_for(self.session.send_request('blockchain.transaction.get', [tx_hash]), 20) + result = await self.session.send_request('blockchain.transaction.get', [tx_hash]) tx = Transaction(result) try: tx.deserialize()