Skip to content

Commit 595badb

Browse files
authored
lifecycle node dtor shutdown should be called only in primary state. (ros2#2544)
* 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. (ros2#2545) Signed-off-by: Tomoya Fujita <[email protected]> --------- Signed-off-by: Tomoya Fujita <[email protected]>
1 parent f95ac7c commit 595badb

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

rclcpp_lifecycle/src/lifecycle_node.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,32 @@ LifecycleNode::LifecycleNode(
133133

134134
LifecycleNode::~LifecycleNode()
135135
{
136-
// shutdown if necessary to avoid leaving the device in unknown state
137-
if (LifecycleNode::get_current_state().id() !=
138-
lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED)
139-
{
140-
auto ret = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
141-
auto finalized = LifecycleNode::shutdown(ret);
142-
if (finalized.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED ||
143-
ret != rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS)
144-
{
145-
RCLCPP_WARN(
136+
auto current_state = LifecycleNode::get_current_state().id();
137+
// shutdown if necessary to avoid leaving the device in any other primary state
138+
if (current_state < lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED) {
139+
if (node_base_->get_context()->is_valid()) {
140+
auto ret = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
141+
auto finalized = LifecycleNode::shutdown(ret);
142+
if (finalized.id() != lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED ||
143+
ret != rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS)
144+
{
145+
RCLCPP_WARN(
146+
rclcpp::get_logger("rclcpp_lifecycle"),
147+
"Shutdown error in destruction of LifecycleNode: final state(%s)",
148+
finalized.label().c_str());
149+
}
150+
} else {
151+
// TODO(fujitatomoya): consider when context is gracefully shutdown before.
152+
RCLCPP_DEBUG(
146153
rclcpp::get_logger("rclcpp_lifecycle"),
147-
"Shutdown error in destruction of LifecycleNode: final state(%s)",
148-
finalized.label().c_str());
154+
"Context invalid error in destruction of LifecycleNode: Node still in transition state(%u)",
155+
current_state);
149156
}
157+
} else if (current_state > lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED) {
158+
RCLCPP_WARN(
159+
rclcpp::get_logger("rclcpp_lifecycle"),
160+
"Shutdown error in destruction of LifecycleNode: Node still in transition state(%u)",
161+
current_state);
150162
}
151163

152164
// release sub-interfaces in an order that allows them to consult with node_base during tear-down
@@ -159,6 +171,7 @@ LifecycleNode::~LifecycleNode()
159171
node_timers_.reset();
160172
node_logging_.reset();
161173
node_graph_.reset();
174+
node_base_.reset();
162175
}
163176

164177
const char *

0 commit comments

Comments
 (0)