simplyprint: suppress write exceptions
Its possible for the websocket client to disconnect before read_message() returns None. Await all calls to write_message() to handle websocket closed exceptions. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
f047167b3b
commit
5d856b9c49
|
@ -22,6 +22,7 @@ from utils import LocalQueueHandler
|
||||||
|
|
||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
|
Awaitable,
|
||||||
Coroutine,
|
Coroutine,
|
||||||
Optional,
|
Optional,
|
||||||
Dict,
|
Dict,
|
||||||
|
@ -1044,7 +1045,7 @@ class SimplyPrint(Subscribable):
|
||||||
def _check_setup_event(self, evt_name: str) -> bool:
|
def _check_setup_event(self, evt_name: str) -> bool:
|
||||||
return self.is_set_up or evt_name in PRE_SETUP_EVENTS
|
return self.is_set_up or evt_name in PRE_SETUP_EVENTS
|
||||||
|
|
||||||
def send_sp(self, evt_name: str, data: Any) -> asyncio.Future:
|
def send_sp(self, evt_name: str, data: Any) -> Awaitable[bool]:
|
||||||
if (
|
if (
|
||||||
not self.connected or
|
not self.connected or
|
||||||
self.ws is None or
|
self.ws is None or
|
||||||
|
@ -1055,19 +1056,21 @@ class SimplyPrint(Subscribable):
|
||||||
fut.set_result(False)
|
fut.set_result(False)
|
||||||
return fut
|
return fut
|
||||||
packet = {"type": evt_name, "data": data}
|
packet = {"type": evt_name, "data": data}
|
||||||
if evt_name != "stream":
|
return self.eventloop.create_task(self._send_wrapper(packet))
|
||||||
self._logger.info(f"sent: {packet}")
|
|
||||||
else:
|
async def _send_wrapper(self, packet: Dict[str, Any]) -> bool:
|
||||||
self._logger.info("sent: webcam stream")
|
|
||||||
self._reset_keepalive()
|
|
||||||
try:
|
try:
|
||||||
fut = self.ws.write_message(json.dumps(packet))
|
assert self.ws is not None
|
||||||
except tornado.websocket.WebSocketClosedError:
|
await self.ws.write_message(json.dumps(packet))
|
||||||
fut = self.eventloop.create_future()
|
except Exception:
|
||||||
fut.set_result(False)
|
return False
|
||||||
else:
|
else:
|
||||||
|
if packet["type"] != "stream":
|
||||||
|
self._logger.info(f"sent: {packet}")
|
||||||
|
else:
|
||||||
|
self._logger.info("sent: webcam stream")
|
||||||
self._reset_keepalive()
|
self._reset_keepalive()
|
||||||
return fut
|
return True
|
||||||
|
|
||||||
def _reset_keepalive(self):
|
def _reset_keepalive(self):
|
||||||
if self.keepalive_hdl is not None:
|
if self.keepalive_hdl is not None:
|
||||||
|
@ -1096,10 +1099,7 @@ class SimplyPrint(Subscribable):
|
||||||
self.webcam_stream.stop()
|
self.webcam_stream.stop()
|
||||||
self.amb_detect.stop()
|
self.amb_detect.stop()
|
||||||
self.printer_info_timer.stop()
|
self.printer_info_timer.stop()
|
||||||
try:
|
await self.send_sp("shutdown", None)
|
||||||
await self.send_sp("shutdown", None)
|
|
||||||
except tornado.websocket.WebSocketClosedError:
|
|
||||||
pass
|
|
||||||
self._logger.close()
|
self._logger.close()
|
||||||
self.is_closing = True
|
self.is_closing = True
|
||||||
if self.ws is not None:
|
if self.ws is not None:
|
||||||
|
|
Loading…
Reference in New Issue