Skip to content

Commit

Permalink
Shield disconnect from cancellation
Browse files Browse the repository at this point in the history
Relevant issue: #3942.
  • Loading branch information
Lonami committed Oct 3, 2022
1 parent 908375a commit ad2238e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion telethon/client/telegrambaseclient.py
Expand Up @@ -598,7 +598,11 @@ def disconnect(self: 'TelegramClient'):
await client.disconnect()
"""
if self.loop.is_running():
return self._disconnect_coro()
# Disconnect may be called from an event handler, which would
# cancel itself during itself and never actually complete the
# disconnection. Shield the task to prevent disconnect itself
# from being cancelled. See issue #3942 for more details.
return asyncio.shield(self.loop.create_task(self._disconnect_coro()))
else:
try:
self.loop.run_until_complete(self._disconnect_coro())
Expand Down

0 comments on commit ad2238e

Please sign in to comment.