From 63563c412cdc9902ba4325fcf7da174c9e32a834 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Wed, 17 Oct 2018 14:32:33 -0300 Subject: [PATCH] cancel pending requests and close connection on server switch --- torba/basenetwork.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/torba/basenetwork.py b/torba/basenetwork.py index 5a62115a7..1fdd1a55c 100644 --- a/torba/basenetwork.py +++ b/torba/basenetwork.py @@ -2,6 +2,7 @@ import logging from itertools import cycle from aiorpcx import ClientSession as BaseClientSession +from asyncio import CancelledError from torba import __version__ from torba.stream import StreamController @@ -59,10 +60,13 @@ class BaseNetwork: log.info("Successfully connected to SPV wallet server: %s", connection_string) self._on_connected_controller.add(True) await self.client.on_disconnected.first - except Exception: # pylint: disable=broad-except + except (Exception, CancelledError): # pylint: disable=broad-except log.exception("Connecting to %s raised an exception:", connection_string) if not self.running: return + elif self.client: + await self.client.close() + self.client.connection.cancel_pending_requests() async def stop(self): self.running = False