🥅 virtual: recalculate wait timeout after gcode eval

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
This commit is contained in:
Gina Häußge committed 2023-11-28 11:27:08 +01:00
1 parent 33525e46f9
commit fb27d5ba64
1 file changed
+14 -3
@@ -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):