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

refactor: wait for message #447

Merged
merged 4 commits into from
Mar 7, 2025
Merged

Conversation

maciejmajek
Copy link
Member

@maciejmajek maciejmajek commented Mar 5, 2025

Purpose

The 2.0 architecture lacked topic discovery mechanism making synthetic tests harder.

Proposed Changes

New, internal implementation of wait_for_message which seems more stable than the rclpy version.

Testing

CI/pytest

Summary by CodeRabbit

  • New Features

    • Introduced a configurable retry mechanism for message reception, enhancing reliability for communications.
  • Refactor

    • Streamlined the message handling process by transitioning to a connector-based approach, simplifying message retrieval and cleanup procedures.
  • Tests

    • Updated several tests by removing failure expectations, reflecting improvements in asynchronous topic discovery and overall test stability.

@maciejmajek maciejmajek requested a review from boczekbartek March 5, 2025 14:51
refactor: use custom wait_for_message method in ros2 topic api instead of flawed wait_for_message from rclpy
@maciejmajek maciejmajek force-pushed the refactor/wait_for_message branch from 0aeba73 to d81fff0 Compare March 7, 2025 12:00
@maciejmajek
Copy link
Member Author

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Mar 7, 2025

Error: Could not generate a valid Mermaid diagram after multiple attempts.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/rai_core/rai/communication/ros2/api.py (1)

294-302: Consider improving topic name normalization.

The topic availability check strips all forward slashes from topic names when comparing. This could potentially cause issues with complex topic hierarchies.

- topic = topic.replace("/", "")
- if topic in [te[0].replace("/", "") for te in topic_endpoints]:
+ # Normalize topic names by ensuring they start with a single '/'
+ normalized_topic = '/' + topic.strip('/')
+ if normalized_topic in [te[0] for te in topic_endpoints] or topic in [te[0] for te in topic_endpoints]:

This would properly normalize topic names while maintaining compatibility with both forms (with and without leading slash).

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7cf12dd and d81fff0.

📒 Files selected for processing (5)
  • src/rai_core/rai/communication/ros2/api.py (3 hunks)
  • tests/communication/ros2/test_api.py (0 hunks)
  • tests/communication/ros2/test_connectors.py (0 hunks)
  • tests/messages/test_transport.py (2 hunks)
  • tests/tools/ros2/test_topic_tools.py (0 hunks)
💤 Files with no reviewable changes (3)
  • tests/communication/ros2/test_connectors.py
  • tests/tools/ros2/test_topic_tools.py
  • tests/communication/ros2/test_api.py
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-test-ros2 (humble)
🔇 Additional comments (7)
tests/messages/test_transport.py (4)

23-23: Good import addition.

The import ROS2ARIConnector from rai.communication.ros2.connectors aligns with the PR objective to refactor message handling.


99-101: Improved implementation using ROS2ARIConnector.

The connector approach is a better design choice than the previous RaiBaseNode implementation, providing a cleaner API for message reception and management.


106-106: Simplified message reception API.

Replacing get_raw_message_from_topic with receive_message provides a more intuitive interface. This change aligns with the PR objective to create a more stable implementation.


109-109: Proper resource cleanup.

Adding connector.shutdown() ensures that resources are properly cleaned up, which is important for preventing resource leaks in tests.

src/rai_core/rai/communication/ros2/api.py (3)

209-209: Good addition of retry mechanism.

Adding the retry_count parameter allows for more robust message reception, especially in environments where message delivery might be inconsistent or delayed.


245-263: Well-implemented topic verification and retry logic.

The implementation now properly verifies topic availability before attempting to receive messages, and uses a retry mechanism to improve reliability. This addresses the PR objective to create a more stable implementation.

One minor concern: the timeout for each retry attempt is divided by the retry count, which means individual attempts get shorter timeouts as retry_count increases. This is a reasonable trade-off to maintain the overall timeout budget.


265-293: Well-implemented message waiting mechanism.

The _wait_for_message method is a clean implementation that:

  1. Creates a subscription with a proper callback
  2. Polls for messages within the timeout period
  3. Properly cleans up the subscription before returning

This custom implementation provides better control over the message waiting process compared to relying on external libraries.

Copy link
Member

@boczekbartek boczekbartek left a comment

Choose a reason for hiding this comment

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

NOTE:

rai git:(refactor/wait_for_message) ✗ rg wait_for_message
src/rai_core/rai/communication/ros2/api.py
251:            success, msg = self._wait_for_message(
265:    def _wait_for_message(

src/rai_core/rai/tools/utils.py
46:# Copied from https://github.com/ros2/rclpy/blob/jazzy/rclpy/rclpy/wait_for_message.py, to support humble
47:def wait_for_message(
213:        success, msg = wait_for_message(

src/rai_core/rai/tools/ros/deprecated.py
34:from rai.tools.ros.utils import wait_for_message
68:        success, msg = wait_for_message(

src/rai_core/rai/tools/ros/utils.py
106:# Copied from https://github.com/ros2/rclpy/blob/jazzy/rclpy/rclpy/wait_for_message.py, to support humble
107:def wait_for_message(

I can see that wait_for_message is only used in deprecated.py now.
Then deprecated.SingleMessageGrabber used only by GetOccupanceGridTool, which is not used anywhere.

This PR is a good enabler to to clean up the deprecated code. But I'm fine with doing it in another PR.

@maciejmajek maciejmajek merged commit eab9152 into development Mar 7, 2025
5 checks passed
@maciejmajek maciejmajek deleted the refactor/wait_for_message branch March 7, 2025 14:06
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.

2 participants