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:
parent
a4abec43d5
commit
31dd758d52
|
@ -738,14 +738,17 @@ class KlippyConnection:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def _read_stream(self, reader: asyncio.StreamReader) -> None:
|
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:
|
try:
|
||||||
data = await reader.readuntil(b'\x03')
|
data = await reader.readuntil(b'\x03')
|
||||||
except asyncio.IncompleteReadError:
|
except (ConnectionError, asyncio.IncompleteReadError):
|
||||||
break
|
break
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception("Klippy Stream Read Error")
|
logging.exception("Klippy Stream Read Error")
|
||||||
|
errors_remaining -= 1
|
||||||
continue
|
continue
|
||||||
|
errors_remaining = 10
|
||||||
try:
|
try:
|
||||||
decoded_cmd = json.loads(data[:-1])
|
decoded_cmd = json.loads(data[:-1])
|
||||||
self.on_recd(decoded_cmd)
|
self.on_recd(decoded_cmd)
|
||||||
|
@ -772,8 +775,11 @@ class KlippyConnection:
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
async with self.close_mutex:
|
async with self.close_mutex:
|
||||||
if self.writer is not None:
|
if self.writer is not None:
|
||||||
self.writer.close()
|
try:
|
||||||
await self.writer.wait_closed()
|
self.writer.close()
|
||||||
|
await self.writer.wait_closed()
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Error closing Klippy Unix Socket")
|
||||||
self.writer = None
|
self.writer = None
|
||||||
if not self.closed:
|
if not self.closed:
|
||||||
self.closed = True
|
self.closed = True
|
||||||
|
|
Loading…
Reference in New Issue