Skip to content

Commit

Permalink
Merge pull request #1739 from mavlink/pr-fix-discovery-deadlock
Browse files Browse the repository at this point in the history
core: fix deadlock on discovery
  • Loading branch information
julianoes authored Apr 18, 2022
2 parents dd039ac + dcbf7b6 commit 22168e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/mavsdk/core/mavsdk_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ MavsdkImpl::~MavsdkImpl()
}

{
std::lock_guard<std::mutex> lock(_systems_mutex);
std::lock_guard<std::recursive_mutex> lock(_systems_mutex);
_systems.clear();
}

Expand Down Expand Up @@ -109,7 +109,7 @@ std::vector<std::shared_ptr<System>> MavsdkImpl::systems() const
{
std::vector<std::shared_ptr<System>> systems_result{};

std::lock_guard<std::mutex> lock(_systems_mutex);
std::lock_guard<std::recursive_mutex> lock(_systems_mutex);
for (auto& system : _systems) {
// We ignore the 0 entry because it's just a null system.
// It's only created because the older, deprecated API needs a
Expand Down Expand Up @@ -212,7 +212,7 @@ void MavsdkImpl::receive_message(mavlink_message_t& message, Connection* connect
return;
}

std::lock_guard<std::mutex> lock(_systems_mutex);
std::lock_guard<std::recursive_mutex> lock(_systems_mutex);

// The only situation where we create a system with sysid 0 is when we initialize the connection
// to the remote.
Expand Down Expand Up @@ -371,7 +371,7 @@ ConnectionResult MavsdkImpl::setup_udp_remote(
if (ret == ConnectionResult::Success) {
new_conn->add_remote(remote_ip, remote_port);
add_connection(new_conn);
std::lock_guard<std::mutex> lock(_systems_mutex);
std::lock_guard<std::recursive_mutex> lock(_systems_mutex);
make_system_with_component(0, 0, true);
}
return ret;
Expand Down Expand Up @@ -500,7 +500,7 @@ void MavsdkImpl::make_system_with_component(

void MavsdkImpl::notify_on_discover()
{
std::lock_guard<std::mutex> lock(_new_system_callback_mutex);
std::lock_guard<std::recursive_mutex> lock(_systems_mutex);
if (_new_system_callback) {
auto temp_callback = _new_system_callback;
call_user_callback([temp_callback]() { temp_callback(); });
Expand All @@ -509,7 +509,7 @@ void MavsdkImpl::notify_on_discover()

void MavsdkImpl::notify_on_timeout()
{
std::lock_guard<std::mutex> lock(_new_system_callback_mutex);
std::lock_guard<std::recursive_mutex> lock(_systems_mutex);
if (_new_system_callback) {
auto temp_callback = _new_system_callback;
call_user_callback([temp_callback]() { temp_callback(); });
Expand All @@ -518,7 +518,7 @@ void MavsdkImpl::notify_on_timeout()

void MavsdkImpl::subscribe_on_new_system(const Mavsdk::NewSystemCallback& callback)
{
std::lock_guard<std::mutex> lock(_new_system_callback_mutex);
std::lock_guard<std::recursive_mutex> lock(_systems_mutex);
_new_system_callback = callback;

if (_new_system_callback != nullptr && is_any_system_connected()) {
Expand Down
5 changes: 1 addition & 4 deletions src/mavsdk/core/mavsdk_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,8 @@ class MavsdkImpl {
std::mutex _connections_mutex{};
std::vector<std::shared_ptr<Connection>> _connections{};

mutable std::mutex _systems_mutex{};

mutable std::recursive_mutex _systems_mutex{};
std::vector<std::pair<uint8_t, std::shared_ptr<System>>> _systems{};

std::mutex _new_system_callback_mutex{};
Mavsdk::NewSystemCallback _new_system_callback{nullptr};

Time _time{};
Expand Down

0 comments on commit 22168e7

Please sign in to comment.