[Sponsored by CubePilot] Try to fix potential mavlink segfaults on USB disconnect (#26083)

* mavlink: fix potential use-after-free

If a mavlink instance is force stopped, the main thread might be out of
scope and the receiver thread would be doing a use-after-free.

Instead the receiver thread needs to check its own _should_exit flag.

* mavlink: protect shared data by mutex in dtor

I'm not sure if this potentially fixes any of the segfaults we have seen
on stopping mavlink instances but it potentially could matter if the
mavlink_receiver thread is killed after a timeout and tries to send any
messages as a zombie.
This commit is contained in:
Julian Oes 2025-12-13 10:24:02 +13:00 committed by GitHub
parent b92d21bd31
commit 5fe82aa485
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 2 deletions

View File

@ -163,7 +163,10 @@ Mavlink::~Mavlink()
}
if (_instance_id >= 0) {
mavlink_module_instances[_instance_id] = nullptr;
{
LockGuard lg{mavlink_module_mutex};
mavlink_module_instances[_instance_id] = nullptr;
}
mavlink_instance_count.fetch_sub(1);
}

View File

@ -3160,7 +3160,7 @@ MavlinkReceiver::run()
ssize_t nread = 0;
hrt_abstime last_send_update = 0;
while (!_mavlink.should_exit()) {
while (!_should_exit.load()) {
// check for parameter updates
if (_parameter_update_sub.updated()) {