From f519218d3ed646609d5273ac73c151f54e6790fe Mon Sep 17 00:00:00 2001 From: Christopher Wecht Date: Wed, 25 Jan 2023 15:43:16 +0100 Subject: [PATCH] Fix potential deadlock in ResourceManager --- hardware_interface/src/resource_manager.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/hardware_interface/src/resource_manager.cpp b/hardware_interface/src/resource_manager.cpp index 7e88cca9b51..64aee65a6d2 100644 --- a/hardware_interface/src/resource_manager.cpp +++ b/hardware_interface/src/resource_manager.cpp @@ -601,8 +601,7 @@ void ResourceManager::load_urdf(const std::string & urdf, bool validate_interfac { if (individual_hardware_info.type == actuator_type) { - std::lock_guard guard(resource_interfaces_lock_); - std::lock_guard guard_claimed(claimed_command_interfaces_lock_); + std::scoped_lock guard(resource_interfaces_lock_, claimed_command_interfaces_lock_); resource_storage_->load_and_initialize_actuator(individual_hardware_info); } if (individual_hardware_info.type == sensor_type) @@ -612,8 +611,7 @@ void ResourceManager::load_urdf(const std::string & urdf, bool validate_interfac } if (individual_hardware_info.type == system_type) { - std::lock_guard guard(resource_interfaces_lock_); - std::lock_guard guard_claimed(claimed_command_interfaces_lock_); + std::scoped_lock guard(resource_interfaces_lock_, claimed_command_interfaces_lock_); resource_storage_->load_and_initialize_system(individual_hardware_info); } } @@ -675,8 +673,7 @@ bool ResourceManager::state_interface_is_available(const std::string & name) con void ResourceManager::import_controller_reference_interfaces( const std::string & controller_name, std::vector & interfaces) { - std::lock_guard guard(resource_interfaces_lock_); - std::lock_guard guard_claimed(claimed_command_interfaces_lock_); + std::scoped_lock guard(resource_interfaces_lock_, claimed_command_interfaces_lock_); auto interface_names = resource_storage_->add_command_interfaces(interfaces); resource_storage_->controllers_reference_interfaces_map_[controller_name] = interface_names; } @@ -730,8 +727,7 @@ void ResourceManager::remove_controller_reference_interfaces(const std::string & resource_storage_->controllers_reference_interfaces_map_.at(controller_name); resource_storage_->controllers_reference_interfaces_map_.erase(controller_name); - std::lock_guard guard(resource_interfaces_lock_); - std::lock_guard guard_claimed(claimed_command_interfaces_lock_); + std::scoped_lock guard(resource_interfaces_lock_, claimed_command_interfaces_lock_); resource_storage_->remove_command_interfaces(interface_names); }