mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 13:29:47 +00:00
Revert disconnect() to be async again (#1133)
It's the only way to properly clean all background tasks, which the library makes heavy use for in MTProto/Connection send and receive loops. Some parts of the code even relied on the fact that it was asynchronous (it used to return a future so you could await it and not be breaking). It's automatically syncified to reduce the damage of being a breaking change.
This commit is contained in:
@@ -440,7 +440,7 @@ class AuthMethods(MessageParseMethods, UserMethods):
|
||||
self._state = types.updates.State(
|
||||
0, 0, datetime.datetime.now(tz=datetime.timezone.utc), 0, 0)
|
||||
|
||||
self.disconnect()
|
||||
await self.disconnect()
|
||||
self.session.delete()
|
||||
return True
|
||||
|
||||
@@ -550,9 +550,9 @@ class AuthMethods(MessageParseMethods, UserMethods):
|
||||
return await self.start()
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.disconnect()
|
||||
self.disconnect() # It's also syncified, like start()
|
||||
|
||||
async def __aexit__(self, *args):
|
||||
self.disconnect()
|
||||
await self.disconnect()
|
||||
|
||||
# endregion
|
||||
|
@@ -281,7 +281,7 @@ class DownloadMethods(UserMethods):
|
||||
if exported:
|
||||
await self._return_exported_sender(sender)
|
||||
elif sender != self._sender:
|
||||
sender.disconnect()
|
||||
await sender.disconnect()
|
||||
if isinstance(file, str) or in_memory:
|
||||
f.close()
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import sys
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from .. import version, __name__ as __base_name__
|
||||
from .. import version, helpers, __name__ as __base_name__
|
||||
from ..crypto import rsa
|
||||
from ..extensions import markdown
|
||||
from ..network import MTProtoSender, ConnectionTcpFull, TcpMTProxy
|
||||
@@ -376,28 +376,29 @@ class TelegramBaseClient(abc.ABC):
|
||||
"""
|
||||
Disconnects from Telegram.
|
||||
|
||||
Returns a dummy completed future with ``None`` as a result so
|
||||
you can ``await`` this method just like every other method for
|
||||
consistency or compatibility.
|
||||
If the event loop is already running, this method returns a
|
||||
coroutine that you should await on your own code; otherwise
|
||||
the loop is ran until said coroutine completes.
|
||||
"""
|
||||
self._disconnect()
|
||||
if self._loop.is_running():
|
||||
return self._disconnect_coro()
|
||||
else:
|
||||
self._loop.run_until_complete(self._disconnect_coro())
|
||||
|
||||
async def _disconnect_coro(self):
|
||||
await self._disconnect()
|
||||
self.session.set_update_state(0, self._state)
|
||||
self.session.close()
|
||||
|
||||
result = self._loop.create_future()
|
||||
result.set_result(None)
|
||||
return result
|
||||
|
||||
def _disconnect(self):
|
||||
async def _disconnect(self):
|
||||
"""
|
||||
Disconnect only, without closing the session. Used in reconnections
|
||||
to different data centers, where we don't want to close the session
|
||||
file; user disconnects however should close it since it means that
|
||||
their job with the client is complete and we should clean it up all.
|
||||
"""
|
||||
self._sender.disconnect()
|
||||
if self._updates_handle:
|
||||
self._updates_handle.cancel()
|
||||
await self._sender.disconnect()
|
||||
await helpers._cancel(self._log, updates_handle=self._updates_handle)
|
||||
|
||||
async def _switch_dc(self, new_dc):
|
||||
"""
|
||||
@@ -412,7 +413,7 @@ class TelegramBaseClient(abc.ABC):
|
||||
self._sender.auth_key.key = None
|
||||
self.session.auth_key = None
|
||||
self.session.save()
|
||||
self._disconnect()
|
||||
await self._disconnect()
|
||||
return await self.connect()
|
||||
|
||||
def _auth_key_callback(self, auth_key):
|
||||
@@ -515,7 +516,7 @@ class TelegramBaseClient(abc.ABC):
|
||||
if not n:
|
||||
self._log[__name__].info(
|
||||
'Disconnecting borrowed sender for DC %d', dc_id)
|
||||
sender.disconnect()
|
||||
await sender.disconnect()
|
||||
|
||||
async def _get_cdn_client(self, cdn_redirect):
|
||||
"""Similar to ._borrow_exported_client, but for CDNs"""
|
||||
|
@@ -16,7 +16,7 @@ class UpdateMethods(UserMethods):
|
||||
try:
|
||||
await self.disconnected
|
||||
except KeyboardInterrupt:
|
||||
self.disconnect()
|
||||
await self.disconnect()
|
||||
|
||||
def run_until_disconnected(self):
|
||||
"""
|
||||
@@ -33,7 +33,7 @@ class UpdateMethods(UserMethods):
|
||||
try:
|
||||
return self.loop.run_until_complete(self.disconnected)
|
||||
except KeyboardInterrupt:
|
||||
self.disconnect()
|
||||
self.loop.run_until_complete(self.disconnect())
|
||||
|
||||
def on(self, event):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user