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

Return result of future from spin_node_until_future_complete #107

Merged
merged 1 commit into from
Sep 14, 2015

Conversation

jacquelinekay
Copy link
Contributor

This is a proposed API change to make spin_until_future_complete less prone to blocking errors.

The previous behavior of the function was to block until the future was complete or if rclcpp was interrupt (i.e. by ctrl-C), and return the future object it was waiting on when complete.

This led to ros2/rmw_connext#97, where the user could not ctrl-C out of the client//service example because accessing the future object caused the thread to block. However, after a ctrl-C the future would never complete because the executor stopped executing.

Instead of returning a shared_future object, the new behavior is to return a return code indicating SUCCESS, INTERRUPTED, or TIMEOUT. If SUCCESS, the future is safe to access without blocking. Also, this PR implements a timeout for the function.

This PR also checks the future before calling a potentially blocking spin function, to prevent indefinite blocking on rmw_wait in case a response already came in before spin_node_until_future_complete was called.

@jacquelinekay jacquelinekay added the in progress Actively being worked on (Kanban column) label Sep 10, 2015
@jacquelinekay jacquelinekay self-assigned this Sep 10, 2015
@jacquelinekay jacquelinekay added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Sep 10, 2015
@esteve
Copy link
Member

esteve commented Sep 10, 2015

Not so sure about changing the API to return a copy of the future result, instead of the future itself. This would fix the issue too:

{
  // Check the future before entering the while loop.
  // If the future is already complete, don't try to spin.
  std::future_status status = future.wait_for(std::chrono::seconds(0));

  while (rclcpp::utilities::ok()) {
    if (status == std::future_status::ready) {
      return future;
    }
    executor.spin_node_once(node_ptr);
    status = future.wait_for(std::chrono::seconds(0));
  }
  // Raise exception
  std::runtime_error("Ctrl-C pressed or an error");
}

this way we can still copy the future around, but still address the Ctrl-C signal. What do you think?

We should add support for timeouts too at some point.

@jacquelinekay
Copy link
Contributor Author

Hmm, are there other places where we throw an exception if rclcpp::ok() is false? In the other spin functions it's simply used as a signal to exit from the function. I'm hesitant to do it because there doesn't seem to be a precedent for it. Receiving the interrupt signal doesn't feel like unexpected behavior to me, but I guess it usually means the program is going to end anyway...

Can we get a third opinion on this? @wjwwood maybe?

@esteve
Copy link
Member

esteve commented Sep 10, 2015

@jacquelinekay I don't think there's another place where a spin function throws an exception. However extending this to add support for timeouts, we'd have to notify call points whether the spin function returned because:

  • the future is valid
  • the timeout was reached
  • the Ctrl-C signal was triggered

via either an exception or an object that represents the status, so returning the result of the future wouldn't be feasible.

@jacquelinekay
Copy link
Contributor Author

Hmm, if we end up introducing timeouts then I think the status code option is the best one, so that we can differentiate between the three different completion states.

@tfoote
Copy link
Contributor

tfoote commented Sep 11, 2015

I agree that a return code makes sense.

@jacquelinekay jacquelinekay added in progress Actively being worked on (Kanban column) and removed in review Waiting for review (Kanban column) labels Sep 11, 2015
@jacquelinekay
Copy link
Contributor Author

Alright, I'll throw this back In Progress until I've prototyped a new return type for this function.

@jacquelinekay
Copy link
Contributor Author

Ok, I prototyped a "FutureReturnCode" enum, changed the function to return that type, and implemented the timeout argument. I wasn't exactly sure how to modify the parameter code for this new change; I throw exceptions in most cases where spin_until_future_complete does not return a success.

@jacquelinekay jacquelinekay added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Sep 11, 2015
@esteve
Copy link
Member

esteve commented Sep 11, 2015

+1, it'd great to add tests for this.

@jacquelinekay
Copy link
Contributor Author

Jenkins looks fine:

http://ci.ros2.org/job/ros2_batch_ci_linux/343/
http://ci.ros2.org/job/ros2_batch_ci_osx/262/
http://ci.ros2.org/job/ros2_batch_ci_windows/396/

Unless there are objections, I'm going to squash, merge, and make an issue for adding tests.

@esteve
Copy link
Member

esteve commented Sep 14, 2015

+1

jacquelinekay added a commit that referenced this pull request Sep 14, 2015
Return result of future from spin_node_until_future_complete
@jacquelinekay jacquelinekay merged commit e2841c9 into master Sep 14, 2015
@jacquelinekay jacquelinekay removed the in review Waiting for review (Kanban column) label Sep 14, 2015
@jacquelinekay jacquelinekay deleted the fix_future branch September 14, 2015 17:26
nnmm pushed a commit to ApexAI/rclcpp that referenced this pull request Jul 9, 2022
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this pull request Aug 5, 2022
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
mauropasse pushed a commit to mauropasse/rclcpp that referenced this pull request Feb 17, 2023
…og-failed-request

Add logs on failed take response/request
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.

None yet

3 participants