Skip to content

Commit c146c8f

Browse files
committed
mavproxy.py: Clean up log_writer exit
* Use threading.Event instead of daemon thread to avoid uncaught exception on ctrl+C exit * Daemon threads are not recommended anymore Signed-off-by: Ryan Friedman <25047695+Ryanf55@users.noreply.github.com>
1 parent 5127953 commit c146c8f

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

MAVProxy/mavproxy.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self):
8787
self.mav_error = 0
8888
self.altitude = 0
8989
self.last_distance_announce = 0.0
90-
self.exit = False
90+
self.stop_event = threading.Event()
9191
self.flightmode = 'MAV'
9292
self.last_mode_announce = 0
9393
self.last_mode_announced = 'MAV'
@@ -807,7 +807,7 @@ def process_stdin(line):
807807
print("%-15s : %s" % (cmd, help))
808808
return
809809
if cmd == 'exit' and mpstate.settings.requireexit:
810-
mpstate.status.exit = True
810+
mpstate.status.stop_event.set()
811811
return
812812

813813
if cmd not in command_map:
@@ -936,8 +936,12 @@ def mkdir_p(dir):
936936

937937
def log_writer():
938938
'''log writing thread'''
939-
while not mpstate.status.exit:
940-
mpstate.logfile_raw.write(bytearray(mpstate.logqueue_raw.get()))
939+
while not mpstate.status.stop_event.is_set():
940+
if not mpstate.logqueue_raw.empty():
941+
bytes = mpstate.logqueue_raw.get(block=False)
942+
mpstate.logfile_raw.write(bytearray(bytes))
943+
944+
# TODO consider wait() the stop event instead
941945
timeout = time.time() + 10
942946
while not mpstate.logqueue_raw.empty() and time.time() < timeout:
943947
mpstate.logfile_raw.write(mpstate.logqueue_raw.get())
@@ -1009,18 +1013,17 @@ def open_telemetry_logs(logpath_telem, logpath_telem_raw):
10091013
stat = os.statvfs(logpath_telem)
10101014
if stat.f_bfree*stat.f_bsize < 209715200:
10111015
print("ERROR: Not enough free disk space for logfile")
1012-
mpstate.status.exit = True
1016+
mpstate.status.stop_event.set()
10131017
return
10141018

10151019
# use a separate thread for writing to the logfile to prevent
10161020
# delays during disk writes (important as delays can be long if camera
10171021
# app is running)
10181022
t = threading.Thread(target=log_writer, name='log_writer')
1019-
t.daemon = True
10201023
t.start()
10211024
except Exception as e:
10221025
print("ERROR: opening log file for writing: %s" % e)
1023-
mpstate.status.exit = True
1026+
mpstate.status.stop_event.set()
10241027
return
10251028

10261029

@@ -1121,7 +1124,7 @@ def main_loop():
11211124
set_stream_rates()
11221125

11231126
while True:
1124-
if mpstate is None or mpstate.status.exit:
1127+
if mpstate is None or mpstate.status.stop_event.is_set():
11251128
return
11261129

11271130
# enable or disable screensaver:
@@ -1218,12 +1221,12 @@ def main_loop():
12181221

12191222
def input_loop():
12201223
'''wait for user input'''
1221-
while mpstate.status.exit is not True:
1224+
while not mpstate.status.stop_event.is_set():
12221225
try:
12231226
line = mpstate.rl.input()
12241227
mpstate.input_queue.put(line)
12251228
except (EOFError, IOError):
1226-
mpstate.status.exit = True
1229+
mpstate.status.stop_event.set()
12271230

12281231

12291232
def run_script(scriptfile):
@@ -1407,7 +1410,6 @@ def run_startup_scripts():
14071410

14081411
# global mavproxy state
14091412
mpstate = MPState()
1410-
mpstate.status.exit = False
14111413
mpstate.command_map = command_map
14121414
mpstate.continue_mode = opts.continue_mode
14131415
# queues for logging
@@ -1447,11 +1449,11 @@ def run_startup_scripts():
14471449

14481450
def quit_handler(signum=None, frame=None):
14491451
# print('Signal handler called with signal', signum)
1450-
if mpstate.status.exit:
1452+
if mpstate.status.stop_event.is_set():
14511453
print('Clean shutdown impossible, forcing an exit')
14521454
sys.exit(0)
14531455
else:
1454-
mpstate.status.exit = True
1456+
mpstate.status.stop_event.set()
14551457

14561458
# Listen for kill signals to cleanly shutdown modules
14571459
fatalsignals = [signal.SIGTERM]
@@ -1584,7 +1586,7 @@ def quit_handler(signum=None, frame=None):
15841586

15851587
# use main program for input. This ensures the terminal cleans
15861588
# up on exit
1587-
while (mpstate.status.exit is not True):
1589+
while not mpstate.status.stop_event.is_set():
15881590
try:
15891591
if opts.daemon or opts.non_interactive:
15901592
time.sleep(0.1)
@@ -1606,7 +1608,7 @@ def quit_handler(signum=None, frame=None):
16061608
m.init(mpstate)
16071609

16081610
else:
1609-
mpstate.status.exit = True
1611+
mpstate.status.stop_event.set()
16101612
sys.exit(1)
16111613

16121614
if opts.profile:

0 commit comments

Comments
 (0)