Treat OSError.errno == EBADF as a ConnectionResetError

This commit is contained in:
Lonami Exo 2017-09-03 11:54:26 +02:00
parent f8f389c850
commit 68cdc9eaf1

View File

@ -71,9 +71,13 @@ class TcpClient:
self._socket.sendall(data) self._socket.sendall(data)
except socket.timeout as e: except socket.timeout as e:
raise TimeoutError() from e raise TimeoutError() from e
except OSError as e:
if e.errno == errno.EBADF:
self._raise_connection_reset()
else:
raise
except BrokenPipeError: except BrokenPipeError:
self.close() self._raise_connection_reset()
raise
def read(self, size): def read(self, size):
"""Reads (receives) a whole block of 'size bytes """Reads (receives) a whole block of 'size bytes
@ -92,11 +96,14 @@ class TcpClient:
partial = self._socket.recv(bytes_left) partial = self._socket.recv(bytes_left)
except socket.timeout as e: except socket.timeout as e:
raise TimeoutError() from e raise TimeoutError() from e
except OSError as e:
if e.errno == errno.EBADF:
self._raise_connection_reset()
else:
raise
if len(partial) == 0: if len(partial) == 0:
self.close() self._raise_connection_reset()
raise ConnectionResetError(
'The server has closed the connection.')
buffer.write(partial) buffer.write(partial)
bytes_left -= len(partial) bytes_left -= len(partial)
@ -104,3 +111,7 @@ class TcpClient:
# If everything went fine, return the read bytes # If everything went fine, return the read bytes
buffer.flush() buffer.flush()
return buffer.raw.getvalue() 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.')