Skip to content

Commit

Permalink
mavproxy.py: Clean up log_writer exit
Browse files Browse the repository at this point in the history
* Use threading.Event instead of daemon thread

Signed-off-by: Ryan Friedman <25047695+Ryanf55@users.noreply.github.com>
  • Loading branch information
Ryanf55 committed Dec 16, 2024
1 parent 5127953 commit 515e063
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions MAVProxy/mavproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import json
import os
import platform
import queue
import select
import serial
import shlex
Expand Down Expand Up @@ -87,7 +88,10 @@ def __init__(self):
self.mav_error = 0
self.altitude = 0
self.last_distance_announce = 0.0
# True when thread should exit, deprecated
self.exit = False
# A newer thread event to stop MAVProxy
self.stop_event = threading.Event()
self.flightmode = 'MAV'
self.last_mode_announce = 0
self.last_mode_announced = 'MAV'
Expand Down Expand Up @@ -936,8 +940,16 @@ def mkdir_p(dir):

def log_writer():
'''log writing thread'''
while not mpstate.status.exit:
mpstate.logfile_raw.write(bytearray(mpstate.logqueue_raw.get()))
print("Starting logs thread...")
while not mpstate.status.stop_event.is_set():
try:
bytes = mpstate.logqueue_raw.get(block=False)
except queue.Empty:
pass
else:
mpstate.logfile_raw.write(bytearray(bytes))

# TODO consider wait() the stop event instead
timeout = time.time() + 10
while not mpstate.logqueue_raw.empty() and time.time() < timeout:
mpstate.logfile_raw.write(mpstate.logqueue_raw.get())
Expand All @@ -946,6 +958,7 @@ def log_writer():
if mpstate.settings.flushlogs or time.time() >= timeout:
mpstate.logfile.flush()
mpstate.logfile_raw.flush()
print("log_writer finished cleanly")


# If state_basedir is NOT set then paths for logs and aircraft
Expand Down Expand Up @@ -1012,11 +1025,10 @@ def open_telemetry_logs(logpath_telem, logpath_telem_raw):
mpstate.status.exit = True
return

# use a separate thread for writing to the logfile to prevent
# Use a separate thread for writing to the logfile to prevent
# delays during disk writes (important as delays can be long if camera
# app is running)
# app is running).
t = threading.Thread(target=log_writer, name='log_writer')
t.daemon = True
t.start()
except Exception as e:
print("ERROR: opening log file for writing: %s" % e)
Expand Down Expand Up @@ -1452,6 +1464,8 @@ def quit_handler(signum=None, frame=None):
sys.exit(0)
else:
mpstate.status.exit = True
mpstate.status.stop_event.set()
#TODO

# Listen for kill signals to cleanly shutdown modules
fatalsignals = [signal.SIGTERM]
Expand Down Expand Up @@ -1584,7 +1598,7 @@ def quit_handler(signum=None, frame=None):

# use main program for input. This ensures the terminal cleans
# up on exit
while (mpstate.status.exit is not True):
while (not mpstate.status.stop_event.is_set()):
try:
if opts.daemon or opts.non_interactive:
time.sleep(0.1)
Expand All @@ -1606,7 +1620,7 @@ def quit_handler(signum=None, frame=None):
m.init(mpstate)

else:
mpstate.status.exit = True
mpstate.status.stop_event.set()
sys.exit(1)

if opts.profile:
Expand Down

0 comments on commit 515e063

Please sign in to comment.