diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 370f228d..1f2f98a0 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -320,24 +320,6 @@ class TelegramBaseClient(abc.ABC): self.api_id = int(api_id) self.api_hash = api_hash - # Current proxy implementation requires `sock_connect`, and some - # event loops lack this method. If the current loop is missing it, - # bail out early and suggest an alternative. - # - # TODO A better fix is obviously avoiding the use of `sock_connect` - # - # See https://github.com/LonamiWebs/Telethon/issues/1337 for details. - if not callable(getattr(self.loop, 'sock_connect', None)): - raise TypeError( - 'Event loop of type {} lacks `sock_connect`, which is needed to use proxies.\n\n' - 'Change the event loop in use to use proxies:\n' - '# https://github.com/LonamiWebs/Telethon/issues/1337\n' - 'import asyncio\n' - 'asyncio.set_event_loop(asyncio.SelectorEventLoop())'.format( - self.loop.__class__.__name__ - ) - ) - if local_addr is not None: if use_ipv6 is False and ':' in local_addr: raise TypeError( @@ -537,6 +519,24 @@ class TelegramBaseClient(abc.ABC): elif self._loop != helpers.get_running_loop(): raise RuntimeError('The asyncio event loop must not change after connection (see the FAQ for details)') + # Current proxy implementation requires `sock_connect`, and some + # event loops lack this method. If the current loop is missing it, + # bail out early and suggest an alternative. + # + # TODO A better fix is obviously avoiding the use of `sock_connect` + # + # See https://github.com/LonamiWebs/Telethon/issues/1337 for details. + if not callable(getattr(self._loop, 'sock_connect', None)): + raise TypeError( + 'Event loop of type {} lacks `sock_connect`, which is needed to use proxies.\n\n' + 'Change the event loop in use to use proxies:\n' + '# https://github.com/LonamiWebs/Telethon/issues/1337\n' + 'import asyncio\n' + 'asyncio.set_event_loop(asyncio.SelectorEventLoop())'.format( + self._loop.__class__.__name__ + ) + ) + # ':' in session.server_address is True if it's an IPv6 address if (not self.session.server_address or (':' in self.session.server_address) != self._use_ipv6): diff --git a/telethon/network/mtprotosender.py b/telethon/network/mtprotosender.py index 59cfab6c..c45e79dd 100644 --- a/telethon/network/mtprotosender.py +++ b/telethon/network/mtprotosender.py @@ -70,8 +70,7 @@ class MTProtoSender: # pending futures should be cancelled. self._user_connected = False self._reconnecting = False - self._disconnected = helpers.get_running_loop().create_future() - self._disconnected.set_result(None) + self.__disconnected = None # We need to join the loops upon disconnection self._send_loop_handle = None @@ -217,6 +216,13 @@ class MTProtoSender: """ return asyncio.shield(self._disconnected) + @property + def _disconnected(self): + if self.__disconnected is None: + self.__disconnected = helpers.get_running_loop().create_future() + self.__disconnected.set_result(None) + return self.__disconnected + # Private methods async def _connect(self): @@ -274,7 +280,7 @@ class MTProtoSender: # or errors after which the sender cannot continue such # as failing to reconnect or any unexpected error. if self._disconnected.done(): - self._disconnected = loop.create_future() + self.__disconnected = loop.create_future() self._log.info('Connection to %s complete!', self._connection)