Repository navigation
Read the matched counts from the DDS endpoints, and skip a publish that nobody would receive - #85
Draft
johannesschrimpf wants to merge 5 commits into
Draft
johannesschrimpf wants to merge 5 commits into
johannesschrimpf wants to merge 5 commits into
Conversation
Publisher::get_subscription_count and Subscription::get_publisher_count read two maps that the Spinner kept from the participant's status events. That had four weaknesses: - without a running Spinner the counts never changed; - a publisher or subscription that had never matched logged an error on every call; - the participant's status channel drops events when it is full, and a dropped match was never repaired; - the maps shrank only on ReaderLost and WriterLost, so a count stayed up when a whole participant was lost. Both now return the current count of the endpoint's own PUBLICATION_MATCHED / SUBSCRIPTION_MATCHED status, which RustDDS keeps where the match happens. The Node parameter is not used any more. It stays for compatibility, and because the Zenoh backend needs it. The maps remain for wait_for_subscription and wait_for_publisher. Assisted-by: Claude Fable 5.2
The counts no longer come from the Node, so a Publisher or Subscription can report them alone: Publisher::matched_subscription_count() and Subscription::matched_publisher_count(). get_subscription_count and get_publisher_count keep their Node parameter and return the same values. This is for code that holds the endpoint where the Node is not at hand, for example a thread that only has the Publisher and wants to check for subscribers before it builds each message. Assisted-by: Claude Fable 5.2
Publisher::publish and async_publish serialized and wrote every message, also while no subscription was matched. For a volatile topic such a message reaches nobody: it is not kept for subscriptions that match later. Both now return Ok without writing in that case, before the message is serialized. They read the matched count that the DataWriter keeps, so the check takes no lock. Nothing is skipped for a transient-local topic, which keeps its messages for late joiners, or for a publisher with manual liveliness, where a write is what asserts it. Assisted-by: Claude Fable 5.2
matched_counts_need_no_node checked that the publisher counts no subscription only after it had created the subscription. Discovery may already have matched the two by then, so the check could fail on a slow machine. With a pause of two seconds before it, it does. Check the count before the listening node and its subscription are created, as counts_follow_a_match_without_a_spinner does. Assisted-by: Claude Fable 5.2 Assisted-by: Astra GPT-6.2
The documentation of matched_subscription_count suggested to use it to not build a message that nobody would receive, without naming a condition. That is right only where publish would drop the message too: on a volatile topic, for a publisher with automatic liveliness. A transient-local topic keeps the message for later subscriptions, and with manual liveliness a write is what asserts it. It also said that the call takes no lock. The count is now read behind one in RustDDS, so say only that the call is cheap. Assisted-by: Claude Fable 5.2 Assisted-by: Astra GPT-6.2
johannesschrimpf
marked this pull request as ready for review
November 19, 2026 16:50
johannesschrimpf
marked this pull request as draft
November 19, 2026 16:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We are porting a set of rclcpp nodes to ros2-client on an i.MX6, and along the way we ran into something that I think is worth fixing here rather than in every application.
One of our nodes publishes about 1500 messages a second, and most of them go to topics that nobody is subscribed to most of the time. In C++ that was nearly free, because rclcpp (with intra-process communication on) does not do the DDS write when there is no subscriber outside the process. With ros2-client every one of those messages is serialized and written.
Our first idea was to check
get_subscription_countbefore publishing. That did not work for us: it logs an error on every call for a publisher that has never matched, and the count comes from a status channel that can drop events. So we polled RustDDS's discovery database instead. That worked, but it cost about 54 MB/s of extra memory reads on this board, and it is a workaround that everybody with the same problem would have to write again.So this PR does three things. It makes
get_subscription_countandget_publisher_countread counts that cannot get lost. It makes the same counts available without aNode. And it stopspublishfrom serializing and writing a message that no subscription would receive.Depends on
Atostek/RustDDS#473, which adds
get_publication_matched_statusandget_subscription_matched_status. This does not build until a RustDDS release has them, so it is a draft. I will add the version requirement then.What is wrong on master
Publisher::get_subscription_countandSubscription::get_publisher_countread two maps that the Spinner keeps from the participant's status events. That implementation has four weaknesses:ReaderLostandWriterLost, so a count stays up when a whole participant is lost.What changes
First commit: the counts come from the endpoints. Both methods return the current count of the endpoint's own PUBLICATION_MATCHED / SUBSCRIPTION_MATCHED status, which RustDDS keeps where the match happens. They took the
Nodebecause the maps live there: the node looked the endpoint's GUID up in them. The count now comes from the endpoint itself, so the argument is ignored. It stays in the signature so that existing callers compile, and because the Zenoh backend's methods of the same name do use the node. The maps remain forwait_for_subscriptionandwait_for_publisher. The Zenoh backend is not touched.Second commit: the same counts without a
Node.Publisher::matched_subscription_count()andSubscription::matched_publisher_count(), for code that holds the endpoint but not the node and wants to avoid even building a message.Third commit:
publishskips a write that nobody would receive.publishandasync_publishreturnOkwithout writing while no subscription is matched, before the message is serialized. Nothing is skipped for a transient-local topic, which keeps its messages for later subscriptions, or for a publisher with manual liveliness, where a write is what asserts it. This is a change in whatpublishdoes, so it is a commit of its own.Compatibility
MessageInfo::sequence_number()at a subscriber: skipped messages do not use up sequence numbers, so there is no jump across a time without subscribers.publishreturnsOkwhere a reliable publisher could returnWouldBlockbefore its first match, the casetests/pub_and_sub.rstolerates.Serializeimplementation is not called.ros_discovery_info,rosout,parameter_events) are transient-local, so they never skip.Tests
tests/matched_counts.rsruns two nodes without a Spinner.publishandasync_publish, and delivery once a subscription is. A transient-local publisher still serves a late subscription, and a publisher with manual liveliness still writes.ROS_DOMAIN_ID(0 if unset, like the other tests), so that it can run on a network where domain 0 is in use.Built against the RustDDS branch, the new tests pass with and without
--features=security. Clippy is clean with-D warningsfor the DDS backend and for both backends together, and the Zenoh-only build passescargo check.tests/pub_and_sub.rsandtests/late_joiner.rspass in copies that change only the domain. The other existing tests that create a context have not run here: they use domain 0, which is in use on the network I work on. The MSRV job andcargo fmtwith the current nightly have not run either.