mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 19:03:46 +03:00
Don't disconnect() on __del__ (#1133)
Destructors are not guaranteed to run. Despite having good intentions (saving entities even if the user forgets), it should be the user's responsability to cleanly close the client under any circumstances.
This commit is contained in:
parent
2e4476a754
commit
8f302bcdb0
|
@ -381,10 +381,8 @@ class TelegramBaseClient(abc.ABC):
|
||||||
consistency or compatibility.
|
consistency or compatibility.
|
||||||
"""
|
"""
|
||||||
self._disconnect()
|
self._disconnect()
|
||||||
if getattr(self, 'session', None):
|
self.session.set_update_state(0, self._state)
|
||||||
if getattr(self, '_state', None):
|
self.session.close()
|
||||||
self.session.set_update_state(0, self._state)
|
|
||||||
self.session.close()
|
|
||||||
|
|
||||||
result = self._loop.create_future()
|
result = self._loop.create_future()
|
||||||
result.set_result(None)
|
result.set_result(None)
|
||||||
|
@ -397,30 +395,10 @@ class TelegramBaseClient(abc.ABC):
|
||||||
file; user disconnects however should close it since it means that
|
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.
|
their job with the client is complete and we should clean it up all.
|
||||||
"""
|
"""
|
||||||
# All properties may be ``None`` if `__init__` fails, and this
|
self._sender.disconnect()
|
||||||
# method will be called from `__del__` which would crash then.
|
if self._updates_handle:
|
||||||
if getattr(self, '_sender', None):
|
|
||||||
self._sender.disconnect()
|
|
||||||
if getattr(self, '_updates_handle', None):
|
|
||||||
self._updates_handle.cancel()
|
self._updates_handle.cancel()
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
if not self.is_connected() or self.loop.is_closed():
|
|
||||||
return
|
|
||||||
|
|
||||||
# READ THIS IF DISCONNECT IS ASYNC AND A TASK WOULD BE MADE.
|
|
||||||
# Python 3.5.2's ``asyncio`` mod seems to have a bug where it's not
|
|
||||||
# able to close the pending tasks properly, and letting the script
|
|
||||||
# complete without calling disconnect causes the script to trigger
|
|
||||||
# 100% CPU load. Call disconnect to make sure it doesn't happen.
|
|
||||||
try:
|
|
||||||
self.disconnect()
|
|
||||||
except Exception:
|
|
||||||
# Arguably not the best solution, but worth trying if the user
|
|
||||||
# forgot to disconnect; normally this is fine but sometimes it
|
|
||||||
# can fail (https://github.com/LonamiWebs/Telethon/issues/1073)
|
|
||||||
pass
|
|
||||||
|
|
||||||
async def _switch_dc(self, new_dc):
|
async def _switch_dc(self, new_dc):
|
||||||
"""
|
"""
|
||||||
Permanently switches the current connection to the new data center.
|
Permanently switches the current connection to the new data center.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user