Skip to content

Commit

Permalink
Guard against TOCTTOU with rclcpp::ok and rclcpp:spin_some (#459)
Browse files Browse the repository at this point in the history
Fixes #458.

An exception can be thrown if an interrupt occurs between checking rclcpp::ok() and rclcpp::spin_some().

Related to ros2/rclcpp#1066

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
  • Loading branch information
jacobperron committed Jan 21, 2021
1 parent 8e8cc04 commit abc8eec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions test_rclcpp/test/test_client_scope_consistency_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ int main(int argc, char ** argv)
"client_scope", handle_add_two_ints, rmw_qos_profile);

rclcpp::WallRate loop_rate(30);
while (rclcpp::ok()) {
rclcpp::spin_some(node);
loop_rate.sleep();
try {
while (rclcpp::ok()) {
rclcpp::spin_some(node);
loop_rate.sleep();
}
} catch (const rclcpp::exceptions::RCLError & ex) {
RCLCPP_ERROR(node->get_logger(), "failed with %s", ex.what());
}
rclcpp::shutdown();
return 0;
Expand Down
10 changes: 7 additions & 3 deletions test_rclcpp/test/test_client_scope_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ int main(int argc, char ** argv)
"client_scope", handle_add_two_ints);

rclcpp::WallRate loop_rate(30);
while (rclcpp::ok()) {
rclcpp::spin_some(node);
loop_rate.sleep();
try {
while (rclcpp::ok()) {
rclcpp::spin_some(node);
loop_rate.sleep();
}
} catch (const rclcpp::exceptions::RCLError & ex) {
RCLCPP_ERROR(node->get_logger(), "failed with %s", ex.what());
}
rclcpp::shutdown();
return 0;
Expand Down

0 comments on commit abc8eec

Please sign in to comment.