From 68cdc9eaf103d68280545155116516c19593c171 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 3 Sep 2017 11:54:26 +0200 Subject: [PATCH] Treat OSError.errno == EBADF as a ConnectionResetError --- telethon/extensions/tcp_client.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/telethon/extensions/tcp_client.py b/telethon/extensions/tcp_client.py index de3bdcf1..1b98f05c 100644 --- a/telethon/extensions/tcp_client.py +++ b/telethon/extensions/tcp_client.py @@ -71,9 +71,13 @@ class TcpClient: self._socket.sendall(data) except socket.timeout as e: raise TimeoutError() from e + except OSError as e: + if e.errno == errno.EBADF: + self._raise_connection_reset() + else: + raise except BrokenPipeError: - self.close() - raise + self._raise_connection_reset() def read(self, size): """Reads (receives) a whole block of 'size bytes @@ -92,11 +96,14 @@ class TcpClient: partial = self._socket.recv(bytes_left) except socket.timeout as e: raise TimeoutError() from e + except OSError as e: + if e.errno == errno.EBADF: + self._raise_connection_reset() + else: + raise if len(partial) == 0: - self.close() - raise ConnectionResetError( - 'The server has closed the connection.') + self._raise_connection_reset() buffer.write(partial) bytes_left -= len(partial) @@ -104,3 +111,7 @@ class TcpClient: # If everything went fine, return the read bytes buffer.flush() return buffer.raw.getvalue() + + def _raise_connection_reset(self): + self.close() # Connection reset -> flag as socket closed + raise ConnectionResetError('The server has closed the connection.')