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 transactional state mutex for RecorderImpl class. #1547

Merged

Conversation

fujitatomoya
Copy link
Contributor

address #1529 (comment)

@fujitatomoya fujitatomoya requested a review from a team as a code owner January 22, 2024 04:51
@fujitatomoya fujitatomoya requested review from gbiggs and jhdcs and removed request for a team January 22, 2024 04:51
@fujitatomoya
Copy link
Contributor Author

@MichaelOrlov minor fix, could you take a look when you have time?

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.

@fujitatomoya Thanks for your contribution.
I have a few nitpick findings and suggestions and also have a concern about using recursive_mutex for protecting state transition.

I don't see a reason or rationale for protecting discovery start/stop operations with mutex at all. After some analysis, I came to the conclusion it would be enough to have an atomic stop_discovery_ variable.
I don't see possibilities for race conditions or undefined behavior in discovery start/stop operations even if it would be a public API and could be called from different threads.

If you agree please remove mutex lock in stop_discovery() method and use regular non-recursive mutex for the state_transition_mutex_.

rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
Copy link
Contributor Author

@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.

@MichaelOrlov sorry, i did not send this to you. cached for like a week in my browser... 😓

rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
rosbag2_transport/src/rosbag2_transport/recorder.cpp Outdated Show resolved Hide resolved
@fujitatomoya fujitatomoya force-pushed the issue-1529-recorder-state-mutex branch from 016b530 to 8a67807 Compare March 14, 2024 23:32
Copy link
Contributor Author

@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.

@MichaelOrlov thanks for the fix, lgtm with green CI.

fujitatomoya and others added 6 commits March 18, 2024 12:34
Signed-off-by: Tomoya.Fujita <tomoya.fujita825@gmail.com>
Signed-off-by: Tomoya.Fujita <tomoya.fujita825@gmail.com>
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
- Also use non-recursive mutex for the `state_transition_mutex_`

Signed-off-by: Michael Orlov <michael.orlov@apex.ai>
- Removed mutex lock in pause(), resume() and toggle_paused() methods
and use only atomic value change for `paused_` variable.
- Renamed `state_transition_mutex_` to the
`start_stop_transition_mutex_`.
- Add unit test for `toggle_paused()`

Signed-off-by: Michael Orlov <michael.orlov@apex.ai>
@fujitatomoya fujitatomoya force-pushed the issue-1529-recorder-state-mutex branch from d788074 to 337be35 Compare March 18, 2024 19:34
@fujitatomoya
Copy link
Contributor Author

CI:

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

- Changed type of the paused_ to the std::atomic_uchar

Signed-off-by: Michael Orlov <michael.orlov@apex.ai>
@MichaelOrlov
Copy link
Contributor

@fujitatomoya There is a warning for my "hacky" cast part in the toggle_paused()

/home/morlov/ros2_rolling/src/ros2/rosbag2/rosbag2_transport/src/rosbag2_transport/recorder.cpp:417:24: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  417 |   if (atomic_fetch_xor(reinterpret_cast<std::atomic_uchar *>(&paused_), 1)) {
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Unfortunately, I was not able to find a way how to fix this warning in a good way without changing the type of the paused_ variable to the std::atomic_uchar or without rolling back to the simple if (paused_.load()) {..} else {..} statement.

I've tried to change the type to the std::atomic_uchar and seems backward implicit type conversion from bool to the std::atomic_uchar works fine without warnings and we can continue using paused_ as a boolean variable. See my latest commit 9241cc5

@MichaelOrlov
Copy link
Contributor

Gist: https://gist.githubusercontent.com/MichaelOrlov/5f018e1125c75305282285cfd52d9352/raw/6c7bfb3b37e6ad32d4e0d2a521a41a02f5364390/ros2.repos
BUILD args: --packages-above-and-dependencies rosbag2_transport rosbag2_tests
TEST args: --packages-above rosbag2_transport rosbag2_tests
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/13483

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

@MichaelOrlov MichaelOrlov changed the title add transactional state mutex for RecorderImpl class. Add transactional state mutex for RecorderImpl class. Mar 19, 2024
@fujitatomoya
Copy link
Contributor Author

@MichaelOrlov green light, we are good to go.

@MichaelOrlov MichaelOrlov merged commit fc3b55b into ros2:rolling Mar 19, 2024
13 of 14 checks passed
@fujitatomoya
Copy link
Contributor Author

@MichaelOrlov thanks for the help!

@MichaelOrlov
Copy link
Contributor

@fujitatomoya Likewise! 😉

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

2 participants