From 9dd1e95eae86879b5a86624644caec7007be8968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 24 Jul 2020 18:11:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20a=20potential=20race=20con?= =?UTF-8?q?dition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the comm layer starts sending state changes (e.g. to auto detect) before the constructor returns, self._comm in the printer interface is still None and thus states can't be decoded. Probably part of #3653 --- src/octoprint/printer/standard.py | 1 + src/octoprint/util/comm.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index 63b47c7d5..01901dfb2 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -267,6 +267,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): logging.getLogger("SERIAL").info("serial.log is currently not enabled, you can enable it via Settings > Serial Connection > Log communication to serial.log") self._comm = comm.MachineCom(port, baudrate, callbackObject=self, printerProfileManager=self._printerProfileManager) + self._comm.start() def disconnect(self, *args, **kwargs): """ diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 872c22fdb..d159fec24 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -648,12 +648,17 @@ class MachineCom(object): self._monitoring_active = True self.monitoring_thread = threading.Thread(target=self._monitor, name="comm._monitor") self.monitoring_thread.daemon = True - self.monitoring_thread.start() # sending thread self._send_queue_active = True self.sending_thread = threading.Thread(target=self._send_loop, name="comm.sending_thread") self.sending_thread.daemon = True + + def start(self): + # doing this here instead of __init__ combats a race condition where + # self._comm in the printer interface is still None on first pushs from + # the comm layer during detection + self.monitoring_thread.start() self.sending_thread.start() def __del__(self):