Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add start_canceled bool flag when creating a timer #2194

Closed
wants to merge 3 commits into from

Conversation

alsora
Copy link
Collaborator

@alsora alsora commented May 17, 2023

This can be used to make sure that a timer is canceled before being registered with a node, avoiding the risk of race conditions if an executor tried to immediately trigger it

Consider a situation where a user creates a timer but wants to activate it only at a later moment.

auto my_executor = std::make_shared<SingleThreadedExecutor>();
std::thread spin_thread([my_executor]() {
    my_executor->spin();
});
my_executor->add_node(my_node);
auto timer = rclcpp::create_timer(my_node, ....);
timer->cancel();

As soon as the timer object is created, the rclcpp::create_timer function would add it to the node timers interface, which would result in notifying the executor about this new entity.
In case of thread starvation and/or race conditions, the executor may try to trigger the timer before it has been cancelled.

This could result in problems or crashes if the users were expecting that timer to not be called at that time.

This PR adds a boolean flag to the rclcpp::create_timer functions in order to cancel the timer in a safe way before adding it to the node timers interface.

This can be used to make sure that a timer is canceled before being registered with a node, avoiding the risk of race conditions if an executor tried to immediately trigger it

Signed-off-by: Alberto Soragna <alberto.soragna@gmail.com>
Signed-off-by: Alberto Soragna <alberto.soragna@gmail.com>
Signed-off-by: Alberto Soragna <alberto.soragna@gmail.com>
Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additional test case would be nice to have.

@@ -90,15 +90,17 @@ create_timer(
rclcpp::Clock::SharedPtr clock,
rclcpp::Duration period,
CallbackT && callback,
rclcpp::CallbackGroup::SharedPtr group = nullptr)
rclcpp::CallbackGroup::SharedPtr group = nullptr,
bool start_canceled = false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about the following? just a suggestion.

Suggested change
bool start_canceled = false)
bool auto_start = true)

@@ -319,6 +319,23 @@ TEST_P(TestTimer, test_failures_with_exceptions)
}
}

/// Create a timer that starts as canceled
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be probably better to add the test case that

  1. create canceled timer.
  2. reset timer. (activate)
  3. check timer callback is fired.

@tgroechel
Copy link

Is this a duplicate of #2005?

@alsora
Copy link
Collaborator Author

alsora commented Jun 2, 2023

Thanks for catching it!
Should we close this one and resurrect the other one? Or should I just apply the requested changes here?

@tgroechel
Copy link

tgroechel commented Jun 2, 2023

Should we close this one and resurrect the other one?

From a quick glance, it looks like the linked issue is part of a somewhat connected "web" of timer related issues. Edit* quick search + links below

Timer issues -> PRs:

My only initial concern with keeping the other issue is it seems* a bit stale / older and this one could likely be merged more quickly. Edit* However, they aren't really that old (Dec of last year) and also include rcl, rclpy, & systems_tests changes + include a lot of design discussions.

Or should I just apply the requested changes here?

With either choice (keeping this or the other), I agree with #2194 (comment) that bool auto_start = true is slightly easier to understand/intuitive as it is more semantically inline with/idiomatic of default behavior imo.

@fujitatomoya
Copy link
Collaborator

@alsora how about closing this one and working on these open PRs? sorry i should have told you before...

@alsora alsora closed this Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants