tinterface.got_disconnected: change from Future to Event - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit b1449a0a392bfa0dddf934afa041a4ee0b6995cc DIR parent e365eb0d85fa19acc3c1f37b2346e5178077e212 HTML Author: SomberNight <somber.night@protonmail.com> Date: Thu, 21 Jan 2021 03:11:15 +0100 interface.got_disconnected: change from Future to Event Events are far easier to reason about. Diffstat: M electrum/interface.py | 5 ++--- M electrum/network.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) --- DIR diff --git a/electrum/interface.py b/electrum/interface.py t@@ -351,7 +351,7 @@ class Interface(Logger): def __init__(self, *, network: 'Network', server: ServerAddr, proxy: Optional[dict]): self.ready = asyncio.Future() - self.got_disconnected = asyncio.Future() + self.got_disconnected = asyncio.Event() self.server = server Logger.__init__(self) assert network.config.path t@@ -486,8 +486,7 @@ class Interface(Logger): self.logger.debug(f"(disconnect) trace for {repr(e)}", exc_info=True) finally: await self.network.connection_down(self) - if not self.got_disconnected.done(): - self.got_disconnected.set_result(1) + self.got_disconnected.set() # if was not 'ready' yet, schedule waiting coroutines: self.ready.cancel() return wrapper_func DIR diff --git a/electrum/network.py b/electrum/network.py t@@ -820,20 +820,20 @@ class Network(Logger, NetworkRetryManager[ServerAddr]): assert iface.ready.done(), "interface not ready yet" # try actual request success_fut = asyncio.ensure_future(func(self, *args, **kwargs)) - await asyncio.wait([success_fut, iface.got_disconnected], return_when=asyncio.FIRST_COMPLETED) + await asyncio.wait([success_fut, iface.got_disconnected.wait()], return_when=asyncio.FIRST_COMPLETED) if success_fut.done() and not success_fut.cancelled(): if success_fut.exception(): try: raise success_fut.exception() except RequestTimedOut: await iface.close() - await iface.got_disconnected + await iface.got_disconnected.wait() continue # try again except RequestCorrupted as e: # TODO ban server? iface.logger.exception(f"RequestCorrupted: {e}") await iface.close() - await iface.got_disconnected + await iface.got_disconnected.wait() continue # try again return success_fut.result() # otherwise; try again