mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-25 00:30:50 +08:00
fix(mavlink): close log files in the receiver thread (#29854)
The log handler opens the log file in the receiver thread, but on 'mavlink stop' its destructor runs in the stop task and fclose()s it there. On NuttX a FILE belongs to the task group that opened it: fclose() from another task frees it while it stays linked in the owner's stream list, and the owner frees it again on exit. This double free corrupts the heap when USB is unplugged during a log download (hardfault in mm_free, or load_mon hanging in mallinfo()). Close the files at the end of MavlinkReceiver::run(), in the owning thread. Ported from the v1.18 fix in #29187. Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
1 parent
483f73d19d
commit
f2c3ab45d1
3 files changed
+18
No files matched your search
@@ -122,6 +122,12 @@ void MavlinkLogHandler::state_idle()
|
||||
}
|
||||
}
|
||||
|
||||
void MavlinkLogHandler::close_files()
|
||||
{
|
||||
close_current_entry();
|
||||
_state = LogHandlerState::Idle;
|
||||
}
|
||||
|
||||
void MavlinkLogHandler::close_current_entry()
|
||||
{
|
||||
if (_current_entry.fp) {
|
||||
|
||||
@@ -59,6 +59,15 @@ public:
|
||||
void send();
|
||||
void handle_message(const mavlink_message_t *msg);
|
||||
|
||||
/**
|
||||
* Close any open log file. Must be called from the thread that handles the
|
||||
* log messages (the receiver thread): on NuttX a FILE belongs to the task
|
||||
* group that opened it, and fclose() from another task (e.g. the destructor
|
||||
* running in 'mavlink stop') frees it while it stays linked in the owner's
|
||||
* stream list, which then frees it again on exit (double free).
|
||||
*/
|
||||
void close_files();
|
||||
|
||||
/// True while LOG_DATA is streaming out.
|
||||
bool sending_data() const { return _state == LogHandlerState::SendingData; }
|
||||
|
||||
|
||||
@@ -4283,6 +4283,9 @@ MavlinkReceiver::run()
|
||||
_tune_publisher->publish_next_tune(t);
|
||||
}
|
||||
}
|
||||
|
||||
// Release files opened by this thread while still in its task group (see close_files())
|
||||
_mavlink_log_handler.close_files();
|
||||
}
|
||||
|
||||
MavlinkReceiver::FrameCheck MavlinkReceiver::check_frame(uint8_t framing, const mavlink_message_t &message)
|
||||
|
||||
Reference in new issue
Block a user