Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mavproxy.py: Clean up log_writer exit #1496

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions MAVProxy/mavproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(self):
self.mav_error = 0
self.altitude = 0
self.last_distance_announce = 0.0
self.exit = False
self.stop_event = threading.Event()
self.flightmode = 'MAV'
self.last_mode_announce = 0
self.last_mode_announced = 'MAV'
Expand Down Expand Up @@ -807,7 +807,7 @@ def process_stdin(line):
print("%-15s : %s" % (cmd, help))
return
if cmd == 'exit' and mpstate.settings.requireexit:
mpstate.status.exit = True
mpstate.status.stop_event.set()
return

if cmd not in command_map:
Expand Down Expand Up @@ -936,8 +936,13 @@ def mkdir_p(dir):

def log_writer():
'''log writing thread'''
while not mpstate.status.exit:
mpstate.logfile_raw.write(bytearray(mpstate.logqueue_raw.get()))
while not mpstate.status.stop_event.is_set():
if not mpstate.logqueue_raw.empty():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still appear to be looking at the raw queue later in this function - conflict resolution error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event is the exit event. This statement should read

while we are not supposed to exit:
   use the queue for logging operations

Perhaps you missed the not when reading the code? It's the same behavior as before. Run logging if we aren't exiting.

bytes = mpstate.logqueue_raw.get(block=False)
mpstate.logfile_raw.write(bytearray(bytes))
time.sleep(0.001)

# 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 Down Expand Up @@ -1009,18 +1014,17 @@ def open_telemetry_logs(logpath_telem, logpath_telem_raw):
stat = os.statvfs(logpath_telem)
if stat.f_bfree*stat.f_bsize < 209715200:
print("ERROR: Not enough free disk space for logfile")
mpstate.status.exit = True
mpstate.status.stop_event.set()
return

# 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)
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)
mpstate.status.exit = True
mpstate.status.stop_event.set()
return


Expand Down Expand Up @@ -1121,7 +1125,7 @@ def main_loop():
set_stream_rates()

while True:
if mpstate is None or mpstate.status.exit:
if mpstate is None or mpstate.status.stop_event.is_set():
return

# enable or disable screensaver:
Expand Down Expand Up @@ -1218,12 +1222,12 @@ def main_loop():

def input_loop():
'''wait for user input'''
while mpstate.status.exit is not True:
while not mpstate.status.stop_event.is_set():
try:
line = mpstate.rl.input()
mpstate.input_queue.put(line)
except (EOFError, IOError):
mpstate.status.exit = True
mpstate.status.stop_event.set()


def run_script(scriptfile):
Expand Down Expand Up @@ -1407,7 +1411,6 @@ def run_startup_scripts():

# global mavproxy state
mpstate = MPState()
mpstate.status.exit = False
mpstate.command_map = command_map
mpstate.continue_mode = opts.continue_mode
# queues for logging
Expand Down Expand Up @@ -1447,11 +1450,11 @@ def run_startup_scripts():

def quit_handler(signum=None, frame=None):
# print('Signal handler called with signal', signum)
if mpstate.status.exit:
if mpstate.status.stop_event.is_set():
print('Clean shutdown impossible, forcing an exit')
sys.exit(0)
else:
mpstate.status.exit = True
mpstate.status.stop_event.set()

# Listen for kill signals to cleanly shutdown modules
fatalsignals = [signal.SIGTERM]
Expand Down Expand Up @@ -1584,7 +1587,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 +1609,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