Skip to content

Commit

Permalink
lifecycle node dtor shutdown should be called only in primary state. (#…
Browse files Browse the repository at this point in the history
…2543)

* lifecycle node dtor shutdown should be called only in primary state.

Signed-off-by: Tomoya Fujita <[email protected]>

* LifecycleNode shutdown on dtor only with valid context. (#2545)

Signed-off-by: Tomoya Fujita <[email protected]>

---------

Signed-off-by: Tomoya Fujita <[email protected]>
  • Loading branch information
fujitatomoya authored Jun 6, 2024
1 parent 753a29b commit dceb612
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions rclcpp_lifecycle/src/lifecycle_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,32 @@ LifecycleNode::LifecycleNode(

LifecycleNode::~LifecycleNode()
{
// shutdown if necessary to avoid leaving the device in unknown state
if (LifecycleNode::get_current_state().id() !=
lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED)
{
auto ret = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
auto finalized = LifecycleNode::shutdown(ret);
if (finalized.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED ||
ret != rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS)
{
RCLCPP_WARN(
auto current_state = LifecycleNode::get_current_state().id();
// shutdown if necessary to avoid leaving the device in any other primary state
if (current_state < lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED) {
if (node_base_->get_context()->is_valid()) {
auto ret = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
auto finalized = LifecycleNode::shutdown(ret);
if (finalized.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED ||
ret != rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS)
{
RCLCPP_WARN(
rclcpp::get_logger("rclcpp_lifecycle"),
"Shutdown error in destruction of LifecycleNode: final state(%s)",
finalized.label().c_str());
}
} else {
// TODO(fujitatomoya): consider when context is gracefully shutdown before.
RCLCPP_DEBUG(
rclcpp::get_logger("rclcpp_lifecycle"),
"Shutdown error in destruction of LifecycleNode: final state(%s)",
finalized.label().c_str());
"Context invalid error in destruction of LifecycleNode: Node still in transition state(%u)",
current_state);
}
} else if (current_state > lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED) {
RCLCPP_WARN(
rclcpp::get_logger("rclcpp_lifecycle"),
"Shutdown error in destruction of LifecycleNode: Node still in transition state(%u)",
current_state);
}

// release sub-interfaces in an order that allows them to consult with node_base during tear-down
Expand All @@ -172,6 +184,7 @@ LifecycleNode::~LifecycleNode()
node_timers_.reset();
node_logging_.reset();
node_graph_.reset();
node_base_.reset();
}

const char *
Expand Down

0 comments on commit dceb612

Please sign in to comment.