Don't error when calling disconnect after logout

This commit is contained in:
Lonami Exo 2022-11-27 11:22:30 +01:00
parent 4f51604def
commit 83f13da420
2 changed files with 7 additions and 0 deletions

View File

@ -620,6 +620,7 @@ class AuthMethods:
await self.disconnect() await self.disconnect()
self.session.delete() self.session.delete()
self.session = None
return True return True
async def edit_2fa( async def edit_2fa(

View File

@ -523,6 +523,9 @@ class TelegramBaseClient(abc.ABC):
except OSError: except OSError:
print('Failed to connect') print('Failed to connect')
""" """
if self.session is None:
raise ValueError('TelegramClient instance cannot be reused after logging out')
if not await self._sender.connect(self._connection( if not await self._sender.connect(self._connection(
self.session.server_address, self.session.server_address,
self.session.port, self.session.port,
@ -603,6 +606,9 @@ class TelegramBaseClient(abc.ABC):
# You don't need to use this if you used "with client" # You don't need to use this if you used "with client"
await client.disconnect() await client.disconnect()
""" """
if self.session is None:
return # already logged out and disconnected
if self.loop.is_running(): if self.loop.is_running():
# Disconnect may be called from an event handler, which would # Disconnect may be called from an event handler, which would
# cancel itself during itself and never actually complete the # cancel itself during itself and never actually complete the