Skip to content

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
Atostek:masterfrom
johannesschrimpf:feat/matched-counts-from-dds
Draft

johannesschrimpf wants to merge 5 commits into
Atostek:masterfrom
johannesschrimpf:feat/matched-counts-from-dds

Conversation

@johannesschrimpf

@johannesschrimpf johannesschrimpf commented Nov 19, 2026 •

Copy link
Copy Markdown

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_count before 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_count and get_publisher_count read counts that cannot get lost. It makes the same counts available without a Node. And it stops publish from serializing and writing a message that no subscription would receive.

Depends on

Atostek/RustDDS#473, which adds get_publication_matched_status and get_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_count and Subscription::get_publisher_count read two maps that the Spinner keeps from the participant's status events. That implementation has four weaknesses:

  • Without a running Spinner the counts never change.
  • A publisher or subscription that has never matched logs an error on every call ("not known to node").
  • RustDDS's participant status channel drops events when it is full, and a dropped match is never repaired.
  • The maps shrink only on ReaderLost and WriterLost, 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 Node because 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 for wait_for_subscription and wait_for_publisher. The Zenoh backend is not touched.

Second commit: the same counts without a Node. Publisher::matched_subscription_count() and Subscription::matched_publisher_count(), for code that holds the endpoint but not the node and wants to avoid even building a message.

Third commit: publish skips a write that nobody would receive. publish and async_publish return Ok without 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 what publish does, so it is a commit of its own.

Compatibility

  • API: no signature changes, and two new methods.
  • The counts have the value they had before whenever a Spinner ran and no event was lost. They now also work without a Spinner, go down when a participant is lost, and log nothing.
  • The skip applies only while the DataWriter has no matched reader, on a volatile topic with automatic liveliness. Such a message was never delivered before either: it was sent to nobody, and a subscription that matched later got a GAP for it. What a program can still notice:
    • MessageInfo::sequence_number() at a subscriber: skipped messages do not use up sequence numbers, so there is no jump across a time without subscribers.
    • publish returns Ok where a reliable publisher could return WouldBlock before its first match, the case tests/pub_and_sub.rs tolerates.
    • A Serialize implementation is not called.
  • ros2-client's own publishers (ros_discovery_info, rosout, parameter_events) are transient-local, so they never skip.
  • Dependency: it needs the RustDDS release that has the getters.

Tests

tests/matched_counts.rs runs two nodes without a Spinner.

  • Counts: both follow a match, the subscription count goes back to zero when the subscription is dropped, and the new methods give the same.
  • Skip: a message type that counts its serializations shows none while nobody is matched, for publish and async_publish, and delivery once a subscription is. A transient-local publisher still serves a late subscription, and a publisher with manual liveliness still writes.
  • On master the count tests fail because the counts stay at zero, and the skip tests because every publish serializes.
  • The test takes its domain from 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 warnings for the DDS backend and for both backends together, and the Zenoh-only build passes cargo check. tests/pub_and_sub.rs and tests/late_joiner.rs pass 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 and cargo fmt with the current nightly have not run either.

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
johannesschrimpf marked this pull request as ready for review November 19, 2026 16:50
@johannesschrimpf
johannesschrimpf marked this pull request as draft November 19, 2026 16:51
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.

1 participant