diff --git a/rclcpp/include/rclcpp/context.hpp b/rclcpp/include/rclcpp/context.hpp index 5d7a6bcecf..f9d4118969 100644 --- a/rclcpp/include/rclcpp/context.hpp +++ b/rclcpp/include/rclcpp/context.hpp @@ -320,6 +320,11 @@ class Context : public std::enable_shared_from_this return sub_context; } + /// Return the global logging configure mutex. + RCLCPP_PUBLIC + std::shared_ptr + get_rcl_logging_configure_mutex(); + protected: // Called by constructor and destructor to clean up by finalizing the // shutdown rcl context and preparing for a new init cycle. diff --git a/rclcpp/src/rclcpp/context.cpp b/rclcpp/src/rclcpp/context.cpp index 1918a77a02..fac2712369 100644 --- a/rclcpp/src/rclcpp/context.cpp +++ b/rclcpp/src/rclcpp/context.cpp @@ -364,6 +364,12 @@ Context::interrupt_all_wait_sets() } } +std::shared_ptr +Context::get_rcl_logging_configure_mutex() +{ + return logging_configure_mutex_; +} + void Context::clean_up() { diff --git a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp index f055abaa25..43529089a2 100644 --- a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp +++ b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp @@ -65,10 +65,13 @@ NodeBase::NodeBase( // Create the rcl node and store it in a shared_ptr with a custom destructor. std::unique_ptr rcl_node(new rcl_node_t(rcl_get_zero_initialized_node())); - ret = rcl_node_init( - rcl_node.get(), - node_name.c_str(), namespace_.c_str(), - context_->get_rcl_context().get(), &rcl_node_options); + { + std::lock_guard guard(*(context_->get_rcl_logging_configure_mutex())); + ret = rcl_node_init( + rcl_node.get(), + node_name.c_str(), namespace_.c_str(), + context_->get_rcl_context().get(), &rcl_node_options); + } if (ret != RCL_RET_OK) { // Finalize the interrupt guard condition. finalize_notify_guard_condition(); @@ -123,8 +126,14 @@ NodeBase::NodeBase( node_handle_.reset( rcl_node.release(), - [](rcl_node_t * node) -> void { - if (rcl_node_fini(node) != RCL_RET_OK) { + [this](rcl_node_t * node) -> void { + rcl_ret_t ret; + { + std::lock_guard guard( + *(this->context_->get_rcl_logging_configure_mutex())); + ret = rcl_node_fini(node); + } + if (ret != RCL_RET_OK) { RCUTILS_LOG_ERROR_NAMED( "rclcpp", "Error in destruction of rcl node handle: %s", rcl_get_error_string().str);