Fix Ctrl+C handling in CLI
Ctrl+C was not being properly handled by the CLI, resulting in an unclean shutdown and other unexpected consequences -- e.g. a "./start-mycroft.sh debug" would kill the background processes on exit. The KeyboardException was never being triggered. Now the SIGINT is being captured and Ctrl+C behaves (as intended) just like pressing Ctrl+X.pull/1954/head
parent
cb891ecf2f
commit
b38668a2ef
|
@ -14,6 +14,7 @@
|
||||||
#
|
#
|
||||||
import sys
|
import sys
|
||||||
import io
|
import io
|
||||||
|
import signal
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
from .gui_server import start_qml_gui
|
from .gui_server import start_qml_gui
|
||||||
|
@ -100,11 +101,30 @@ CLR_LOG_CMDMESSAGE = 0
|
||||||
CLR_METER_CUR = 0
|
CLR_METER_CUR = 0
|
||||||
CLR_METER = 0
|
CLR_METER = 0
|
||||||
|
|
||||||
|
# Allow Ctrl+C catching...
|
||||||
|
ctrl_c_was_pressed = False
|
||||||
|
|
||||||
|
|
||||||
|
def ctrl_c_handler(signum, frame):
|
||||||
|
global ctrl_c_was_pressed
|
||||||
|
ctrl_c_was_pressed = True
|
||||||
|
|
||||||
|
|
||||||
|
def ctrl_c_pressed():
|
||||||
|
global ctrl_c_was_pressed
|
||||||
|
if ctrl_c_was_pressed:
|
||||||
|
ctrl_c_was_pressed = False
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
signal.signal(signal.SIGINT, ctrl_c_handler)
|
||||||
|
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
# Helper functions
|
# Helper functions
|
||||||
|
|
||||||
|
|
||||||
def clamp(n, smallest, largest):
|
def clamp(n, smallest, largest):
|
||||||
""" Force n to be between smallest and largest, inclusive """
|
""" Force n to be between smallest and largest, inclusive """
|
||||||
return max(smallest, min(n, largest))
|
return max(smallest, min(n, largest))
|
||||||
|
@ -1151,25 +1171,22 @@ def gui_main(stdscr):
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
set_screen_dirty()
|
set_screen_dirty()
|
||||||
|
c = 0
|
||||||
|
code = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Don't block, this allows us to refresh the screen while
|
if ctrl_c_pressed():
|
||||||
# waiting on initial messagebus connection, etc
|
# User hit Ctrl+C. treat same as Ctrl+X
|
||||||
scr.timeout(1)
|
c = 24
|
||||||
c = scr.get_wch() # unicode char or int for special keys
|
|
||||||
if c == -1:
|
|
||||||
continue
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
# User hit Ctrl+C to quit
|
|
||||||
if find_str:
|
|
||||||
# End the find session
|
|
||||||
find_str = None
|
|
||||||
rebuild_filtered_log()
|
|
||||||
continue # Consumed the Ctrl+C, get next character
|
|
||||||
else:
|
else:
|
||||||
c = 24 # treat as Ctrl+X (Exit)
|
# Don't block, this allows us to refresh the screen while
|
||||||
|
# waiting on initial messagebus connection, etc
|
||||||
|
scr.timeout(1)
|
||||||
|
c = scr.get_wch() # unicode char or int for special keys
|
||||||
|
if c == -1:
|
||||||
|
continue
|
||||||
except curses.error:
|
except curses.error:
|
||||||
# This happens in odd cases, such as when you Ctrl+Z suspend
|
# This happens in odd cases, such as when you Ctrl+Z
|
||||||
# the CLI and then resume. Curses fails on get_wch().
|
# the CLI and then resume. Curses fails on get_wch().
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -1319,7 +1336,11 @@ def gui_main(stdscr):
|
||||||
# End the find session
|
# End the find session
|
||||||
find_str = None
|
find_str = None
|
||||||
rebuild_filtered_log()
|
rebuild_filtered_log()
|
||||||
|
elif line.startswith(":"):
|
||||||
|
# cancel command mode
|
||||||
|
line = ""
|
||||||
else:
|
else:
|
||||||
|
# exit CLI
|
||||||
break
|
break
|
||||||
elif code > 31 and isinstance(c, str):
|
elif code > 31 and isinstance(c, str):
|
||||||
# Accept typed character in the utterance
|
# Accept typed character in the utterance
|
||||||
|
|
Loading…
Reference in New Issue