simplyprint: websocket write fix

Gracefully handle websocket closed conditions.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-07-30 14:50:01 -04:00
parent ea29788ab1
commit e0cf635d34
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 14 additions and 3 deletions

View File

@ -1021,6 +1021,7 @@ class SimplyPrint(Subscribable):
if ( if (
not self.connected or not self.connected or
self.ws is None or self.ws is None or
self.ws.protocol is None or
not self._check_setup_event(evt_name) not self._check_setup_event(evt_name)
): ):
fut = self.eventloop.create_future() fut = self.eventloop.create_future()
@ -1032,7 +1033,14 @@ class SimplyPrint(Subscribable):
else: else:
self._logger.info("sent: webcam stream") self._logger.info("sent: webcam stream")
self._reset_keepalive() self._reset_keepalive()
return self.ws.write_message(json.dumps(packet)) try:
fut = self.ws.write_message(json.dumps(packet))
except tornado.websocket.WebSocketClosedError:
fut = self.eventloop.create_future()
fut.set_result(False)
else:
self._reset_keepalive()
return fut
def _reset_keepalive(self): def _reset_keepalive(self):
if self.keepalive_hdl is not None: if self.keepalive_hdl is not None:
@ -1059,10 +1067,13 @@ class SimplyPrint(Subscribable):
async def close(self): async def close(self):
self.print_handler.cancel() self.print_handler.cancel()
self.webcam_stream.stop() self.webcam_stream.stop()
await self.send_sp("shutdown", None)
self._logger.close()
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)
except tornado.websocket.WebSocketClosedError:
pass
self._logger.close()
self.is_closing = True self.is_closing = True
if self.ws is not None: if self.ws is not None:
self.ws.close(1001, "Client Shutdown") self.ws.close(1001, "Client Shutdown")