diff --git a/electrum/interface.py b/electrum/interface.py index 1bd666511..4583f5185 100644 --- a/electrum/interface.py +++ b/electrum/interface.py @@ -616,7 +616,8 @@ class Interface(Logger): return conn, res['count'] def is_main_server(self) -> bool: - return self.network.default_server == self.server + return (self.network.interface == self or + self.network.interface is None and self.network.default_server == self.server) async def open_session(self, sslc, exit_early=False): session_factory = lambda *args, iface=self, **kwargs: NotificationSession(*args, **kwargs, interface=iface) @@ -678,6 +679,9 @@ class Interface(Logger): await asyncio.sleep(60) async def close(self): + """Closes the connection and waits for it to be closed. + We try to flush buffered data to the wire, so this can take some time. + """ if self.session: await self.session.close() # monitor_connection will cancel tasks diff --git a/electrum/network.py b/electrum/network.py index b006b3c55..328f074e0 100644 --- a/electrum/network.py +++ b/electrum/network.py @@ -682,7 +682,8 @@ class Network(Logger, NetworkRetryManager[ServerAddr]): # However, for headers sub, give preference to this interface # over unknown ones, i.e. start it again right away. if old_server and old_server != server: - await self._close_interface(old_interface) + # don't wait for old_interface to close as that might be slow: + await self.taskgroup.spawn(self._close_interface(old_interface)) if len(self.interfaces) <= self.num_server: await self.taskgroup.spawn(self._run_new_interface(old_server)) @@ -711,8 +712,9 @@ class Network(Logger, NetworkRetryManager[ServerAddr]): with self.interfaces_lock: if self.interfaces.get(interface.server) == interface: self.interfaces.pop(interface.server) - if interface.server == self.default_server: + if interface == self.interface: self.interface = None + # this can take some time if server/connection is slow: await interface.close() @with_recent_servers_lock @@ -729,8 +731,8 @@ class Network(Logger, NetworkRetryManager[ServerAddr]): '''A connection to server either went down, or was never made. We distinguish by whether it is in self.interfaces.''' if not interface: return - server = interface.server - if server == self.default_server: + # note: don't rely on interface.server for comparisons here + if interface == self.interface: self._set_status('disconnected') await self._close_interface(interface) util.trigger_callback('network_updated')