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

Make peek_next_message_from_queue return a SharedPtr. #993

Merged
merged 2 commits into from
Apr 21, 2022

Conversation

clalancette
Copy link
Contributor

It is unsafe to return a pointer to a SharedPtr here,
and then store it; if another thread comes along and
pops the queue, then we are now holding a pointer to an
object that has been freed. Instead, dereference the
pointer so we get a SharedPtr (and a reference) immediately,
which ensures that the object won't be destroyed if we
race with another thread.

While I was in here, did a small refactoring of the method
to remove some duplicated code.

Signed-off-by: Chris Lalancette clalancette@openrobotics.org

Before this PR, if I ran the test_play_seek test in a loop with stress on my machine, it would crash every once in a while. After this PR, I no longer see that crash. This should fix at least one of the flaky tests on the buildfarm.

It is unsafe to return a pointer to a SharedPtr here,
and then store it; if another thread comes along and
pops the queue, then we are now holding a pointer to an
object that has been freed.  Instead, dereference the
pointer so we get a SharedPtr (and a reference) immediately,
which ensures that the object won't be destroyed if we
race with another thread.

While I was in here, did a small refactoring of the method
to remove some duplicated code.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
@clalancette clalancette requested a review from a team as a code owner April 20, 2022 12:27
@clalancette clalancette requested review from emersonknapp and hidmic and removed request for a team April 20, 2022 12:27
@clalancette
Copy link
Contributor Author

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Windows Build Status

Copy link
Contributor

@MichaelOrlov MichaelOrlov left a comment

Choose a reason for hiding this comment

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

@clalancette Thank you for the code clean up.
I agree that using shared pointer instead of pointer to the shared pointer will make code less error prone for the race conditions.

Although I disagree with renaming message_ptr to the message where its corresponds to the shared pointer. It's still a pointer and comparison aka message != null_ptr looks very odd in this case.
Could you please revert such renaming?

@clalancette
Copy link
Contributor Author

Although I disagree with renaming message_ptr to the message where its corresponds to the shared pointer. It's still a pointer and comparison aka message != null_ptr looks very odd in this case. Could you please revert such renaming?

I'll point out that the the current naming is following what was already there. That is, in all cases where we were returning a SharedPtr * before, that was stored in a message_ptr, and then the dereferenced version (SharedPtr) was stored in a message.

That said, I'll go in and rename things to message_ptr, but I'm also going to rename the variable inside of peek_next_message_from_queue to message_ptr_ptr.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
@clalancette
Copy link
Contributor Author

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Windows Build Status

Copy link
Member

@ivanpauno ivanpauno left a comment

Choose a reason for hiding this comment

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

LGTM!

@clalancette
Copy link
Contributor Author

The CI failure on Linux is one of the known flaky tests in rosbag2, i.e. test_play_services sometimes just fails. That's the next one for me to look into.

@clalancette clalancette merged commit ee73e02 into master Apr 21, 2022
@clalancette clalancette deleted the clalancette/fix-player-peek branch April 21, 2022 15:27
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