Skip to content

Commit

Permalink
Gracefully handle user-invoked disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Oct 19, 2023
1 parent 854d374 commit 864d5cd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions client/src/telethon/_impl/client/client/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ async def disconnect(self: Client) -> None:
return
assert self._dispatcher

sender = self._sender
self._sender = None # treated as disconnected

self._dispatcher.cancel()
try:
await self._dispatcher
Expand All @@ -220,11 +223,9 @@ async def disconnect(self: Client) -> None:
self._dispatcher = None

try:
await self._sender.disconnect()
await sender.disconnect()
except Exception:
self._logger.exception("unhandled exception during disconnect; this is a bug")
finally:
self._sender = None

self._session.state = self._message_box.session_state()
await self._storage.save(self._session)
Expand Down Expand Up @@ -272,7 +273,15 @@ async def step_sender(client: Client, sender: Sender, lock: asyncio.Lock) -> Non
pass
else:
async with lock:
updates = await sender.step()
try:
updates = await sender.step()
except ConnectionError:
if client.connected:
raise
else:
# disconnect was called, so the socket returning 0 bytes is expected
return

process_socket_updates(client, updates)


Expand Down

0 comments on commit 864d5cd

Please sign in to comment.