Skip to content

Add new API for the RealtimePublisher #323

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

saikishor
Copy link
Member

This should avoid any dangling threads of the RealtimePublisher and handling lock and unlock is not safe in most of the situation, so, I've added can_publish and try_publish methods

@codecov-commenter
Copy link

codecov-commenter commented May 2, 2025

Codecov Report

Attention: Patch coverage is 86.48649% with 5 lines in your changes missing coverage. Please review.

Project coverage is 87.06%. Comparing base (b79a4fa) to head (1ef422f).

Files with missing lines Patch % Lines
...ools/include/realtime_tools/realtime_publisher.hpp 78.57% 1 Missing and 2 partials ⚠️
realtime_tools/test/realtime_publisher_tests.cpp 91.30% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #323      +/-   ##
==========================================
+ Coverage   86.92%   87.06%   +0.13%     
==========================================
  Files          17       17              
  Lines        1262     1291      +29     
  Branches      102      106       +4     
==========================================
+ Hits         1097     1124      +27     
+ Misses         98       97       -1     
- Partials       67       70       +3     
Flag Coverage Δ
unittests 87.06% <86.48%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
realtime_tools/test/realtime_publisher_tests.cpp 88.57% <91.30%> (+1.33%) ⬆️
...ools/include/realtime_tools/realtime_publisher.hpp 89.79% <78.57%> (+1.42%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@saikishor
Copy link
Member Author

@Juliaj This is the new API, could you please take a look when you have time :)

@christophfroehlich
Copy link
Contributor

should we start migration notes for this repo?

[[deprecated(
"Use the try_publish() method to publish the message instead of using this method. This method "
"may be removed in future versions.")]]
void lock()
Copy link
Contributor

@Juliaj Juliaj May 3, 2025

Choose a reason for hiding this comment

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

We may still need the deprecated lock() and unblock() to handle the logic in
ForceTorqueSensorBroadcaster::on_configure which seems to be different than publishing a msg. In on_configure , only part of a message (e.g., msg_.header) needs modification without immediate publishing.

 realtime_publisher_->lock();
 realtime_publisher_->msg_.header.frame_id = params_.frame_id;
 realtime_publisher_->unlock(); 

An alternative would be making a similar safer version for modifying message field.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for pointing it out. I don't think we would need that, because we can use the get_mutex() and directly use the unique_lock there which is much better than the current approach.

On the other hand, I want the users to not directly access the Msg_ variable and instead pass through the try_publish method by having a local version of message in the controllers etc. In future, I would like to extend it with a publish method where we can use an internal message queue (the size would be defined at the construction time) and this would make sure that the messages are always published. Right now, if it is unable to lock, we can never publish messages.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, with the new get_mutex(), above code could become

{
  std::unique_lock<std::mutex> lock(realtime_publisher_->get_mutex());
  realtime_publisher_->msg_.header.frame_id = params_.frame_id;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

In future, I would like to extend it with a publish method where we can use an internal message queue (the size would be defined at the construction time) and this would make sure that the messages are always published.

This will be really helpful.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes exactly, just like your code snippet

Copy link
Contributor

@Juliaj Juliaj left a comment

Choose a reason for hiding this comment

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

LGTM.

BTW, your fix in stop() for the deadlock issue was made to jazzy branch not master. i am assuming we also need that fix on master.

@saikishor
Copy link
Member Author

LGTM.

BTW, your fix in stop() for the deadlock issue was made to jazzy branch not master. i am assuming we also need that fix on master.

@Juliaj It is already on the master branch
05c13d9

@christophfroehlich
Copy link
Contributor

can we add the gcc pragmas to remove deprecation warnings from building the tests? or should we just remove the tests for the old API?

do you want to backport this to jazzy as well? or better leave the API untouched there? we could also just add the new methods

@saikishor
Copy link
Member Author

can we add the gcc pragmas to remove deprecation warnings from building the tests? or should we just remove the tests for the old API?

do you want to backport this to jazzy as well? or better leave the API untouched there? we could also just add the new methods

Sorry i missed this comment. yes, I would like to backport this to Jazzy

Copy link
Contributor

@christophfroehlich christophfroehlich left a comment

Choose a reason for hiding this comment

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

@christophfroehlich christophfroehlich added the backport-jazzy This label should be used by maintainers only! Label triggers PR backport to ROS 2 humble. label May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-jazzy This label should be used by maintainers only! Label triggers PR backport to ROS 2 humble.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants