From 3cb80991a1326a0f4fd63dbfb3b33ddd9b54292d Mon Sep 17 00:00:00 2001 From: "Dmitry D. Chernov" Date: Sat, 20 May 2017 18:34:05 +1000 Subject: [PATCH] Raise an exception if recv() returned 0 bytes See for details: https://docs.python.org/3/howto/sockets.html "When a recv returns 0 bytes, it means the other side has closed (or is in the process of closing) the connection. You will not receive any more data on this connection. Ever." --- telethon/network/tcp_client.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/telethon/network/tcp_client.py b/telethon/network/tcp_client.py index bb9bc2e9..ae463202 100755 --- a/telethon/network/tcp_client.py +++ b/telethon/network/tcp_client.py @@ -82,6 +82,9 @@ class TcpClient: # This is why we need to keep checking to make sure that we receive it all left_count = buffer_size - writer.written_count partial = self.socket.recv(left_count) + if len(partial) == 0: + raise ConnectionResetError( + 'The server has closed the connection (recv() returned 0 bytes).') writer.write(partial) except BlockingIOError as error: