Skip to content

Implement Executor::cancel#153

Merged
wjwwood merged 8 commits intomasterfrom
cancel
Nov 17, 2015
Merged

Implement Executor::cancel#153
wjwwood merged 8 commits intomasterfrom
cancel

Conversation

@jacquelinekay
Copy link
Copy Markdown
Contributor

implement state and logic for cancelling out of Executor::spin or Executor::spin_some.

@jacquelinekay jacquelinekay added the in progress Actively being worked on (Kanban column) label Nov 13, 2015
@jacquelinekay jacquelinekay self-assigned this Nov 13, 2015
@jacquelinekay jacquelinekay added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Nov 13, 2015
@tfoote
Copy link
Copy Markdown
Contributor

tfoote commented Nov 13, 2015

Seeing this do we have race conditions related to calling spin* from multiple threads? It lookslike I could call spin_some in one thread then cancel, and spin_some in another thread and if the timing is right both will continue to spin, since the cancel has been reset by the spin_some?

People calling spin from multiple threads has been a problem in ROS1

@wjwwood
Copy link
Copy Markdown
Member

wjwwood commented Nov 13, 2015

The spin functions are not thread safe, so I'd say that's unsupported behavior.

@jacquelinekay
Copy link
Copy Markdown
Contributor Author

If users want parallelized execution they should use the MultiThreadedExecutor (for which I will implement spin_some soon). I think that the entry point to spin should always be from a single thread, however.

@jacquelinekay
Copy link
Copy Markdown
Contributor Author

@jacquelinekay
Copy link
Copy Markdown
Contributor Author

Rerunning CI with changes from @wjwwood to ensure spin* cannot be entered by multiple threads at the same time.

http://ci.ros2.org/job/ros2_batch_ci_linux/591/
http://ci.ros2.org/job/ros2_batch_ci_osx/477/
http://ci.ros2.org/job/ros2_batch_ci_windows/652/

@wjwwood
Copy link
Copy Markdown
Member

wjwwood commented Nov 16, 2015

Lgtm, but someone else should review it as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Manually resetting this is not safe when exceptions happen. Since it can't be done in a finally block this should use RAII to ensure that value gets reset to false.

Same below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you suggest setting it to false in the destructor, then?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I refer to calling spinning.exchange(true) before in this function. If an exception happens afterwards it doesn't get set back to false. This should use RAII (like a scoped mutex lock) to ensure setting the value back in any case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it.

I was trying to avoid adding mutexes to Executor where they're not needed. But mutex and lock_guard are the most suited to RAII-style management of this state. Maybe this would work with try_lock instead of lock so that we throw an exception if the Executor is already spinning instead of blocking.

@wjwwood, since you implemented about half of this PR, any thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah I'll send another pr asap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, my first pass at the strategy I suggested was not successful (deadlock) and it makes the code a lot more confusing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I just realized you were expecting me to do this 😄. I totally misread it, but anyways I just pushed my fix. I did it by implementing what's called a scope exit or scope guard. Boost has a scope exit and so does the D language. There's some interesting discussion about including something like this in the c++ std library at some point. Anyways this should work us and might be useful else where too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With this implemented should we remove the redundant setting to false at the end of the scope?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a fair point, I'll push another commit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@wjwwood
Copy link
Copy Markdown
Member

wjwwood commented Nov 17, 2015

@jacquelinekay
Copy link
Copy Markdown
Contributor Author

Lots of Executor test failures. I'll take a look.

@wjwwood
Copy link
Copy Markdown
Member

wjwwood commented Nov 17, 2015

Sorry, I actually ran the tests locally and there was a failing test, I just missed it (I am not a smart man). I fixed the issue, see 1c9eb0b.

@dirk-thomas
Copy link
Copy Markdown
Member

lgtm

@wjwwood
Copy link
Copy Markdown
Member

wjwwood commented Nov 17, 2015

CI looks good.

wjwwood added a commit that referenced this pull request Nov 17, 2015
Implement Executor::cancel
@wjwwood wjwwood merged commit 39c663e into master Nov 17, 2015
@wjwwood wjwwood removed the in review Waiting for review (Kanban column) label Nov 17, 2015
@wjwwood wjwwood deleted the cancel branch November 17, 2015 23:33
nnmm pushed a commit to ApexAI/rclcpp that referenced this pull request Jul 9, 2022
mauropasse added a commit to mauropasse/rclcpp that referenced this pull request Aug 23, 2024
Co-authored-by: Mauro Passerino <mpasserino@irobot.com>
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.

4 participants