From 98b71a587041287dfb6418ceed425b7227d03b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 8 Feb 2018 15:17:12 +0100 Subject: [PATCH] Tell PySerial to cancel reads & writes on connection close Only done if the methods needed for that exist and are callable though (to prevent issues with custom serial port implementations provided through hooks, or operating systems that don't have support for cancel_read/cancel_write in PySerial. Fixes #2333 (at least as far as we'll ever be able to fix it) --- src/octoprint/util/comm.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 5d431d417..8f063bc83 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -736,6 +736,18 @@ class MachineCom(object): deactivate_monitoring_and_send_queue() + try: + if hasattr(self._serial, "cancel_read") and callable(self._serial.cancel_read): + self._serial.cancel_read() + except: + self._logger.exception("Error while cancelling pending reads from the serial port") + + try: + if hasattr(self._serial, "cancel_write") and callable(self._serial.cancel_write): + self._serial.cancel_write() + except: + self._logger.exception("Error while cancelling pending writes to the serial port") + try: self._serial.close() except: