Exit receive loop on IOError or unhandled exceptions

May help with #4088.
This commit is contained in:
Lonami Exo 2023-04-29 12:53:25 +02:00
parent 03ff996ace
commit ccf67d0f4f

View File

@ -253,7 +253,8 @@ class Connection(abc.ABC):
Disconnects from the server, and clears Disconnects from the server, and clears
pending outgoing and incoming messages. pending outgoing and incoming messages.
""" """
self._connected = False if not self._connected:
return
await helpers._cancel( await helpers._cancel(
self._log, self._log,
@ -278,6 +279,8 @@ class Connection(abc.ABC):
# * ConnectionResetError # * ConnectionResetError
self._log.info('%s during disconnect: %s', type(e), e) self._log.info('%s during disconnect: %s', type(e), e)
self._connected = False
def send(self, data): def send(self, data):
""" """
Sends a packet of data through this connection mode. Sends a packet of data through this connection mode.
@ -333,6 +336,7 @@ class Connection(abc.ABC):
except (IOError, asyncio.IncompleteReadError) as e: except (IOError, asyncio.IncompleteReadError) as e:
self._log.warning('Server closed the connection: %s', e) self._log.warning('Server closed the connection: %s', e)
await self._recv_queue.put((None, e)) await self._recv_queue.put((None, e))
await self.disconnect()
except InvalidChecksumError as e: except InvalidChecksumError as e:
self._log.warning('Server response had invalid checksum: %s', e) self._log.warning('Server response had invalid checksum: %s', e)
await self._recv_queue.put((None, e)) await self._recv_queue.put((None, e))
@ -342,6 +346,7 @@ class Connection(abc.ABC):
except Exception as e: except Exception as e:
self._log.exception('Unexpected exception in the receive loop') self._log.exception('Unexpected exception in the receive loop')
await self._recv_queue.put((None, e)) await self._recv_queue.put((None, e))
await self.disconnect()
else: else:
await self._recv_queue.put((data, None)) await self._recv_queue.put((data, None))
except asyncio.CancelledError: except asyncio.CancelledError: