moonraker: unix socket fix

Handle connection errors in the read loop.  Set a maximum number
of consecutive errors encountered during a read before aborting.
Resolves #309.

Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-12-12 06:40:53 -05:00
parent a4abec43d5
commit 31dd758d52
1 changed files with 10 additions and 4 deletions

View File

@ -738,14 +738,17 @@ class KlippyConnection:
return True
async def _read_stream(self, reader: asyncio.StreamReader) -> None:
while not reader.at_eof():
errors_remaining: int = 10
while not reader.at_eof() and errors_remaining:
try:
data = await reader.readuntil(b'\x03')
except asyncio.IncompleteReadError:
except (ConnectionError, asyncio.IncompleteReadError):
break
except Exception:
logging.exception("Klippy Stream Read Error")
errors_remaining -= 1
continue
errors_remaining = 10
try:
decoded_cmd = json.loads(data[:-1])
self.on_recd(decoded_cmd)
@ -772,8 +775,11 @@ class KlippyConnection:
async def close(self) -> None:
async with self.close_mutex:
if self.writer is not None:
self.writer.close()
await self.writer.wait_closed()
try:
self.writer.close()
await self.writer.wait_closed()
except Exception:
logging.exception("Error closing Klippy Unix Socket")
self.writer = None
if not self.closed:
self.closed = True