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

fix segmentation fault when adding multiple executors simultaneously #1632

Conversation

DensoADAS
Copy link
Contributor

While creating multiple SingleThreadedExecturors in different thread I ran in crashes.
By adding a lock for "on_shutdown_callbacks_" in "on_shutdown" which is called in the constructor of SingleThreadedExecturors I solved the issue.

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.

I am not sure if this is related to #1524, but on_shutdown_callbacks_ should be protected by on_shutdown_callbacks_mutex_.

@@ -344,6 +344,7 @@ Context::shutdown(const std::string & reason)
rclcpp::Context::OnShutdownCallback
Context::on_shutdown(OnShutdownCallback callback)
{
std::lock_guard<std::mutex> lock(on_shutdown_callbacks_mutex_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably not only adding mutex here, but also all access to on_shutdown_callbacks_?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is not possible without changing the API as the reference is returned

Copy link
Member

Choose a reason for hiding this comment

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

We should deprecate in the future those getters, returning a reference to an internal container isn't great.

I think that the mutex should also be taken here before calling all the callbacks:

  {
    std::lock_guard<std::mutex> lock(on_shutdown_callbacks_mutex_);
    for (const auto & callback : on_shutdown_callbacks_) {
      callback();
    }
  }

Copy link
Member

Choose a reason for hiding this comment

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

We should deprecate in the future those getters, returning a reference to an internal container isn't great.

In the general case, I don't agree with this, but in this particular case, I agree.

We could return a copy (since they should be trivially copyable) and lock that operation.

Or we could just drop the accessors. I think I added them just because having hidden state like that can bite you later if you need access to it and have to add an API to get it, or work around it by keeping track of them outside this class (duplicating the effort). So I'd lean towards returning a copy and protecting that with the mutex.

@wjwwood
Copy link
Member

wjwwood commented Apr 17, 2021

@DensoADAS can you fix the commit so it passes our DCO check?

@wjwwood
Copy link
Member

wjwwood commented Apr 17, 2021

I can help address the comments, but I cannot fix the DCO thing.

@ivanpauno
Copy link
Member

@DensoADAS friendly ping, could you sign the commit so the DCO check passes? Thanks!

@DensoADAS
Copy link
Contributor Author

Hi, just saw the mail. Do I have to sign off with a different mail/name?

@clalancette
Copy link
Contributor

The DCO bot can be a bit picky at times. Right now it says this:

Expected "Joshua Hampp [email protected]", but got "Joshua Hampp [email protected]".

I think it is unhappy because the author (Joshua Hampp [email protected]) doesn't match the sign-off line (Joshua Hampp [email protected]).

…hich led to a race condition (seg fault) when creating multiple executors (multithreaded)

Signed-off-by: Joshua Hampp <[email protected]>
Signed-off-by: Joshua Hampp <[email protected]>
@DensoADAS DensoADAS force-pushed the dnae_adas/bugfix_racecondition_in_context_creation branch from 6eb34a8 to eb13e5f Compare April 28, 2021 02:45
Signed-off-by: Joshua Hampp <[email protected]>
Signed-off-by: Joshua Hampp <[email protected]>
@DensoADAS DensoADAS force-pushed the dnae_adas/bugfix_racecondition_in_context_creation branch from 61a48a2 to ce7ded4 Compare April 28, 2021 08:04
@ivanpauno
Copy link
Member

Superseded by #1639, which fixed another thing + this.

@ivanpauno ivanpauno closed this May 3, 2021
@ivanpauno
Copy link
Member

Thanks @DensoADAS for the contribution!

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.

5 participants