Don't raise write() and close() exceptions

This commit is contained in:
Dan 2022-12-26 16:23:57 +01:00
parent 7e5d593544
commit 9ee1807e42

View File

@ -93,19 +93,13 @@ class TCP:
await self.send_task await self.send_task
try: try:
self.writer.close() if self.writer is not None:
except AttributeError: self.writer.close()
try: await asyncio.wait_for(self.writer.wait_closed(), TCP.TIMEOUT)
self.socket.shutdown(socket.SHUT_RDWR) except Exception as e:
except OSError: log.info("Close exception: %s %s", type(e).__name__, e)
pass
finally:
# A tiny sleep placed here helps avoiding .recv(n) hanging until the timeout.
# This is a workaround that seems to fix the occasional delayed stop of a client.
time.sleep(0.001)
self.socket.close()
async def send(self, data: Optional[bytes]): async def send(self, data: bytes):
await self.send_queue.put(data) await self.send_queue.put(data)
async def send_worker(self): async def send_worker(self):
@ -115,8 +109,11 @@ class TCP:
if data is None: if data is None:
break break
self.writer.write(data) try:
await self.writer.drain() self.writer.write(data)
await self.writer.drain()
except Exception as e:
log.info("Send exception: %s %s", type(e).__name__, e)
async def recv(self, length: int = 0): async def recv(self, length: int = 0):
data = b"" data = b""