From fb27d5ba6442fded66cdec3fbc5275303de5e730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 28 Nov 2023 11:27:08 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20virtual:=20recalculate=20wait=20?= =?UTF-8?q?timeout=20after=20gcode=20eval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise we might run into a race condition in case of long running commands. For an example see #4907: A `G4 S54` triggered a `wait` response, since right after sending the `ok` the host didn't send a new incoming line fast enough to prevent that from happening. As a result, combined with several simulated transmition errors right after, things get so much out of balance that at some point the incoming queue runs full and an exception gets triggered, killing the connection with a simulated SerialTimeout. This fix should close the core cause of #4907 --- .../plugins/virtual_printer/virtual.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/octoprint/plugins/virtual_printer/virtual.py b/src/octoprint/plugins/virtual_printer/virtual.py index fba3678cd..dc0e3c74a 100644 --- a/src/octoprint/plugins/virtual_printer/virtual.py +++ b/src/octoprint/plugins/virtual_printer/virtual.py @@ -345,7 +345,14 @@ class VirtualPrinter: pass def _processIncoming(self): - next_wait_timeout = time.monotonic() + self._waitInterval + next_wait_timeout = 0 + + def recalculate_next_wait_timeout(): + nonlocal next_wait_timeout + next_wait_timeout = time.monotonic() + self._waitInterval + + recalculate_next_wait_timeout() + buf = b"" while self.incoming is not None and not self._killed: self._simulateTemps() @@ -361,7 +368,7 @@ class VirtualPrinter: except queue.Empty: if self._sendWait and time.monotonic() > next_wait_timeout: self._send("wait") - next_wait_timeout = time.monotonic() + self._waitInterval + recalculate_next_wait_timeout() continue except Exception: if self.incoming is None: @@ -377,7 +384,7 @@ class VirtualPrinter: else: continue - next_wait_timeout = time.monotonic() + self._waitInterval + recalculate_next_wait_timeout() if data is None: continue @@ -527,6 +534,10 @@ class VirtualPrinter: continue finally: + # recalculate the timeout again, as we might have just run a long + # running command + recalculate_next_wait_timeout() + # make sure that the debug sleepAfter and sleepAfterNext stuff works even # if we continued above if len(self._sleepAfter) or len(self._sleepAfterNext):