More robust interrupt handling for MP
@@ -83,6 +83,7 @@ class MicroPythonBackend:
self._connection = connection
self._cwd = None
self._interrupt_requested = False
+ self._cancel_requested = False
self._command_queue = Queue() # populated by reader thread
self._progress_times = {}
@@ -120,6 +121,7 @@ class MicroPythonBackend:
def _mainloop(self):
while True:
try:
+ self._cancel_requested = False
self._interrupt_requested = False
self._check_for_connection_errors()
cmd = self._command_queue.get(timeout=0.1)
@@ -227,11 +229,12 @@ class MicroPythonBackend:
self._connection.write(INTERRUPT_CMD)
def _check_for_interrupt(self, action_scope):
- if self._interrupt_requested:
- if action_scope == "device":
+ if action_scope == "device" and self._interrupt_requested:
self._interrupt()
- else:
- assert action_scope == "local"
+ self._interrupt_requested = False
+
+ if action_scope == "local" and self._cancel_requested:
+ self._cancel_requested = False
raise KeyboardInterrupt()
def _interrupt_to_raw_prompt(self):
@@ -278,6 +281,7 @@ class MicroPythonBackend:
cmd = parse_message(line)
if isinstance(cmd, InterruptCommand):
self._interrupt_requested = True
+ self._cancel_requested = True
else:
self._command_queue.put(cmd)
