From 17516318e6f86ba63ba07916e9dbce583f9090dc Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 21 Sep 2022 10:58:35 +0200 Subject: [PATCH] Add a hard timeout on disconnect Closes #3917. --- telethon/network/connection/connection.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/telethon/network/connection/connection.py b/telethon/network/connection/connection.py index 8bff043d..c9934667 100644 --- a/telethon/network/connection/connection.py +++ b/telethon/network/connection/connection.py @@ -265,7 +265,12 @@ class Connection(abc.ABC): self._writer.close() if sys.version_info >= (3, 7): try: - await self._writer.wait_closed() + await asyncio.wait_for(self._writer.wait_closed(), timeout=10) + except asyncio.TimeoutError: + # See issue #3917. For some users, this line was hanging indefinitely. + # The hard timeout is not ideal (connection won't be properly closed), + # but the code will at least be able to procceed. + self._log.warning('Graceful disconnection timed out, forcibly ignoring cleanup') except Exception as e: # Disconnecting should never raise. Seen: # * OSError: No route to host and