From 01b98b71a2b9e0860cce29f9a6793731b0543f59 Mon Sep 17 00:00:00 2001 From: Johannes Schrimpf Date: Thu, 19 Nov 2026 14:23:52 +0100 Subject: [PATCH 1/2] Do not count best-effort readers as behind in the heartbeat tick Writer::handle_heartbeat_tick() decides whether a reliable Writer sends its periodic HEARTBEAT, and whether the next one comes after the fast or the slow period, by checking that every matched reader proxy has acknowledged the last change. A best-effort reader never sends an ACKNACK, so its proxy stays at all_acked_before == 0 and the check never holds. A reliable Writer with a best-effort reader matched therefore sent a non-final HEARTBEAT to multicast every HEARTBEAT_PERIOD_FAST (100 ms) for as long as that reader was matched, also when every reliable reader had acknowledged everything, and when the best-effort reader was the only one. Reliable writers with best-effort readers are common in ROS 2, e.g. a sensor topic read with the sensor-data QoS profile. RTPS 2.5 lets a reliable Writer be matched with a best-effort Reader (8.4.4). The best-effort Reader behaviors (8.4.11.1, 8.4.12.1) have no HEARTBEAT transition, so such a reader does nothing with these HEARTBEATs; RustDDS's own Reader ignores them too. Fast DDS marks a change acknowledged for a best-effort reader as soon as it is sent (StatefulWriter: acked_changes_set() when !is_reliable()), so its periodic heartbeat does not see such a reader as unacknowledged. The tick now looks at reliable reader proxies only, as refresh_acked_frontier() already does. A manual liveliness assertion (is_manual_assertion) keeps considering every reader, so what it sends is unchanged. Tests, all failing without the change: - best_effort_reader_does_not_keep_the_heartbeat_going - reliable_reader_behind_keeps_the_heartbeat_going_beside_a_best_effort_one - manual_liveliness_assertion_still_considers_best_effort_readers Assisted-by: Claude Fable 5.2 --- src/rtps/writer.rs | 104 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 5 deletions(-) diff --git a/src/rtps/writer.rs b/src/rtps/writer.rs index 181aba36..e25a50d7 100644 --- a/src/rtps/writer.rs +++ b/src/rtps/writer.rs @@ -1074,10 +1074,10 @@ impl Writer { /// This is called periodically. /// - /// Returns `true` if some matched reader still has unacknowledged data (i.e. - /// a HEARTBEAT was sent to prompt repair), so the caller can reschedule the - /// next periodic heartbeat sooner. Returns `false` when all readers are - /// caught up. + /// Returns `true` if some matched reliable reader still has unacknowledged + /// data (i.e. a HEARTBEAT was sent to prompt repair), so the caller can + /// reschedule the next periodic heartbeat sooner. Returns `false` when all + /// reliable readers are caught up. pub fn handle_heartbeat_tick(&mut self, is_manual_assertion: bool) -> bool { if self.like_stateless { info!( @@ -1101,12 +1101,21 @@ impl Writer { let first_change = self.send_buffer.first_change_sequence_number(); let last_change = self.send_buffer.last_change_sequence_number(); + // Only a reliable reader acknowledges. A best-effort reader never sends an + // ACKNACK and ignores HEARTBEATs (RTPS 2.5, 8.4.11.1 and 8.4.12.1: the + // best-effort Reader behaviors have no HEARTBEAT transition), so its proxy + // stays at all_acked_before == 0. It must not count as a reader that is + // behind: it would keep the periodic HEARTBEAT, and the fast period, going + // for as long as it is matched. refresh_acked_frontier() filters the same + // way. A manual liveliness assertion keeps considering every reader, as + // before. if self .readers .values() + .filter(|rp| is_manual_assertion || rp.qos().is_reliable()) .all(|rp| last_change < rp.all_acked_before) { - trace!("heartbeat tick: all readers have all available data."); + trace!("heartbeat tick: all reliable readers have all available data."); false } else { // the interface to .heartbeat_msg is silly: we give ref to ourself @@ -2983,6 +2992,91 @@ mod tests { } } + fn best_effort_qos() -> QosPolicies { + QosPolicyBuilder::new() + .reliability(crate::dds::qos::policy::Reliability::BestEffort) + .build() + } + + // The reader acknowledges everything below `next`. + fn acknowledge_all_before(f: &mut Fixture, next: i64) { + let ack_nack = AckNack { + reader_id: f.reader_guid.entity_id, + writer_id: f.writer.my_guid.entity_id, + reader_sn_state: SequenceNumberSet::from_base_and_set( + SequenceNumber::new(next), + &BTreeSet::new(), + ), + count: 1, + }; + f.writer + .handle_ack_nack(f.reader_guid.prefix, &AckSubmessage::AckNack(ack_nack)); + } + + // A best-effort reader never acknowledges, so it must not keep a reliable + // Writer's periodic HEARTBEAT going, nor hold it on the fast period. + #[test] + fn best_effort_reader_does_not_keep_the_heartbeat_going() { + let writer_qos = QosPolicyBuilder::new() + .reliability(crate::dds::qos::policy::Reliability::Reliable { + max_blocking_time: crate::Duration::from_millis(100), + }) + .history(crate::dds::qos::policy::History::KeepAll) + .durability(Durability::TransientLocal) + .build(); + let mut f = Fixture::new( + EntityId::new([0, 0, 1], EntityKind::WRITER_WITH_KEY_USER_DEFINED), + writer_qos, + EntityId::new([0, 0, 2], EntityKind::READER_WITH_KEY_USER_DEFINED), + &best_effort_qos(), + 3, + 8, + None, + ); + for _ in 0..3 { + assert!( + !f.writer.handle_heartbeat_tick(false), + "a best-effort reader kept the Writer heartbeating" + ); + } + } + + // A reliable reader that is behind still does, with a best-effort reader + // matched beside it, until it has acknowledged everything. + #[test] + fn reliable_reader_behind_keeps_the_heartbeat_going_beside_a_best_effort_one() { + let mut f = Fixture::user(3, 8, None); + let best_effort = GUID::new(GuidPrefix::new(&[3; 12]), f.reader_guid.entity_id); + let qos = best_effort_qos(); + f.writer + .update_reader_proxy(&RtpsReaderProxy::new(best_effort, qos.clone(), false), &qos); + assert!(f.writer.readers.contains_key(&best_effort)); + + assert!( + f.writer.handle_heartbeat_tick(false), + "no HEARTBEAT for a reliable reader that has acknowledged nothing" + ); + acknowledge_all_before(&mut f, 4); + assert!( + !f.writer.handle_heartbeat_tick(false), + "still heartbeating after the reliable reader acknowledged everything" + ); + } + + // A manual liveliness assertion is unchanged: it still considers every + // matched reader. + #[test] + fn manual_liveliness_assertion_still_considers_best_effort_readers() { + let mut f = Fixture::user(3, 8, None); + acknowledge_all_before(&mut f, 4); + let best_effort = GUID::new(GuidPrefix::new(&[3; 12]), f.reader_guid.entity_id); + let qos = best_effort_qos(); + f.writer + .update_reader_proxy(&RtpsReaderProxy::new(best_effort, qos.clone(), false), &qos); + assert!(!f.writer.handle_heartbeat_tick(false)); + assert!(f.writer.handle_heartbeat_tick(true)); + } + // Built-in and user Writers alike answer an ACKNACK only after // nack_response_delay, from the SendRepairData timer. #[test] From eff081d1ca32db5da719df8336e88d916d24d2d3 Mon Sep 17 00:00:00 2001 From: Johannes Schrimpf Date: Thu, 19 Nov 2026 14:27:29 +0100 Subject: [PATCH 2/2] Give a best-effort ParticipantMessage reader a best-effort proxy A remote participant can declare in its SPDP data that its BuiltinParticipantMessageReader is best-effort (builtinEndpointQos, RTPS 2.5 8.4.13.3). DPEventLoop::update_participant() has a special case for that: it sets the reader's QoS to best-effort, so that the local BuiltinParticipantMessageWriter knows not to expect ACKNACKs from it. But the reader proxy was built before the special case, from the QoS of the built-in endpoint list, which is reliable. The changed QoS only went into the QoS compatibility check of update_reader_proxy(). The proxy that the writer keeps said reliable, and the writer goes by its proxies: - refresh_acked_frontier() counted the reader as a reliable reader that has acknowledged nothing. - handle_heartbeat_tick() looks at reliable reader proxies only, since the previous commit. It took this reader for one that is behind, so the periodic HEARTBEAT kept going, at HEARTBEAT_PERIOD_FAST, for as long as the participant was there. Build the proxy after the special case, with the QoS that it sets. Test, failing without the change: - best_effort_participant_message_reader_does_not_keep_the_heartbeat_going: the event loop learns about a participant that declares the reader best-effort. The local BuiltinParticipantMessageWriter is matched with it, and its HEARTBEAT tick finds no reader to wait for. A participant that does not declare it is the control: its reader keeps the HEARTBEAT going until it is lost. The test builds a DPEventLoop without UDP listeners. test_event_loop() is new for that: the event loop tests had no such fixture. Found by code review, not in a capture: no participant in our captures sends PID_BUILTIN_ENDPOINT_QOS. Validation on macOS: - cargo test -- --test-threads=1, with and without --features security - cargo clippy --all-targets -- -D warnings, with and without --features security - cargo +nightly fmt --check - cargo check --all-targets --target armv7-unknown-linux-gnueabihf Assisted-by: Claude Fable 5.2 --- src/rtps/dp_event_loop.rs | 218 +++++++++++++++++++++++++++++++++++++- 1 file changed, 213 insertions(+), 5 deletions(-) diff --git a/src/rtps/dp_event_loop.rs b/src/rtps/dp_event_loop.rs index 715a10a3..ebe64eb8 100644 --- a/src/rtps/dp_event_loop.rs +++ b/src/rtps/dp_event_loop.rs @@ -801,9 +801,6 @@ impl DPEventLoop { .available_builtin_endpoints .contains(*reader_endpoint_set_elem) { - let reader_proxy = - discovered_participant.get_builtin_reader_proxy(*reader_eid, reader_qos); - // Get the QoS for the built-in topic from the local writer let mut reader_qos = reader_qos.clone(); @@ -821,6 +818,12 @@ impl DPEventLoop { // `writer` knows not to expect them, and avoid stalling. }; + // The writer goes by the QoS in its reader proxy when it decides + // whether to wait for a reader's ACKNACKs, so the proxy has to be + // built with the reliability set above. + let reader_proxy = + discovered_participant.get_builtin_reader_proxy(*reader_eid, &reader_qos); + writer.update_reader_proxy(&reader_proxy, &reader_qos); debug!( "update_discovery writer - endpoint {:?} - {:?}", @@ -1277,13 +1280,22 @@ mod tests { use super::*; use crate::{ dds::{ + ddsdata::DDSData, qos::QosPolicies, - statusevents::{sync_status_channel, DataReaderStatus}, + statusevents::{sync_status_channel, DataReaderStatus, DataWriterStatus}, typedesc::TypeDesc, - with_key::simpledatareader::ReaderCommand, + with_key::{datawriter::WriteOptions, simpledatareader::ReaderCommand}, + }, + discovery::{ + builtin_endpoint::BuiltinEndpointQos, discovery::Discovery, discovery_db::discovery_db_write, + spdp_participant_data::SpdpDiscoveredParticipantData, }, + messages::submessages::elements::serialized_payload::SerializedPayload, mio_source, polling::{ready_channel, wake_channel, wake_channel_unbounded, LoopWaker}, + rtps::writer_send_buffer::{Admission, WriterSendBuffer}, + test::test_data::spdp_participant_data, + RepresentationIdentifier, }; //#[test] @@ -1429,6 +1441,202 @@ mod tests { child.join().unwrap(); } + // A DPEventLoop without UDP listeners. The channel ends that the test does + // not use stay open, because the event loop stops or logs errors when they + // close. + fn test_event_loop(participant_guid: GUID) -> DPEventLoop { + let waker = LoopWaker::new(); + let (add_reader_sender, add_reader_receiver) = wake_channel::(8, &waker); + let (remove_reader_sender, remove_reader_receiver) = wake_channel::(4, &waker); + let (add_writer_sender, add_writer_receiver) = wake_channel::(8, &waker); + let (remove_writer_sender, remove_writer_receiver) = wake_channel::(4, &waker); + let (stop_poll_sender, stop_poll_receiver) = wake_channel_unbounded(&waker); + let (discovery_update_notification_sender, discovery_update_notification_receiver) = + wake_channel(8, &waker); + let discovery_waker = LoopWaker::new(); + let (discovery_command_sender, discovery_command_receiver) = + wake_channel::(64, &discovery_waker); + let (spdp_liveness_sender, spdp_liveness_receiver) = wake_channel(8, &discovery_waker); + let (reader_ready_sender, reader_ready) = ready_channel(&waker); + let (writer_ready_sender, writer_ready) = ready_channel(&waker); + let (participant_status_sender, participant_status_receiver) = sync_status_channel(16).unwrap(); + let (discovery_db_event_sender, discovery_db_event_receiver) = std::sync::mpsc::sync_channel(4); + std::mem::forget(( + add_reader_sender, + remove_reader_sender, + add_writer_sender, + remove_writer_sender, + stop_poll_sender, + discovery_update_notification_sender, + discovery_command_receiver, + spdp_liveness_receiver, + reader_ready_sender, + writer_ready_sender, + participant_status_receiver, + discovery_db_event_receiver, + )); + + let discovery_db = Arc::new(RwLock::new(DiscoveryDB::new( + participant_guid, + discovery_db_event_sender, + participant_status_sender.clone(), + ))); + DPEventLoop::new( + DomainInfo { + domain_participant_guid: participant_guid, + domain_id: 0, + participant_id: 0, + }, + Arc::new(RwLock::new(DDSCache::new())), + HashMap::new(), + discovery_db, + participant_guid.prefix, + TokenReceiverPair { + token: ADD_READER_TOKEN, + receiver: add_reader_receiver, + }, + TokenReceiverPair { + token: REMOVE_READER_TOKEN, + receiver: remove_reader_receiver, + }, + TokenReceiverPair { + token: ADD_WRITER_TOKEN, + receiver: add_writer_receiver, + }, + TokenReceiverPair { + token: REMOVE_WRITER_TOKEN, + receiver: remove_writer_receiver, + }, + stop_poll_receiver, + discovery_update_notification_receiver, + discovery_command_sender, + spdp_liveness_sender, + participant_status_sender, + None, + None, + 0, + true, + reader_ready, + writer_ready, + waker, + ) + .expect("DPEventLoop::new in test") + } + + // SPDP data of a remote participant that has the built-in endpoints of the + // test data. It has no locators, so nothing is sent to it. + fn remote_participant( + guid_prefix: GuidPrefix, + builtin_endpoint_qos: Option, + ) -> SpdpDiscoveredParticipantData { + let mut data = spdp_participant_data().unwrap(); + data.participant_guid = GUID::new(guid_prefix, EntityId::PARTICIPANT); + data.metatraffic_unicast_locators.clear(); + data.metatraffic_multicast_locators.clear(); + data.default_unicast_locators.clear(); + data.default_multicast_locators.clear(); + data.builtin_endpoint_qos = builtin_endpoint_qos; + data + } + + // A remote participant can declare in its SPDP data that its + // BuiltinParticipantMessageReader is best-effort (builtinEndpointQos, RTPS + // 2.5 Section 8.4.13.3). Such a reader never acknowledges. The local + // BuiltinParticipantMessageWriter has to know it as best-effort. If it took + // the reader for a reliable one, it would wait for its acknowledgments: the + // periodic HEARTBEAT would keep going, at the fast period, for as long as the + // participant is there. + #[test] + fn best_effort_participant_message_reader_does_not_keep_the_heartbeat_going() { + use speedy::{Endianness, Readable}; + + let participant_guid = GUID::new_participant_guid(); + let mut event_loop = test_event_loop(participant_guid); + + // The local BuiltinParticipantMessageWriter, which holds one sample + let writer_guid = GUID::new( + participant_guid.prefix, + EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER, + ); + let send_buffer = WriterSendBuffer::new( + writer_guid, + "DCPSParticipantMessage".to_string(), + true, // reliable + true, // built-in + false, // TransientLocal + DEFAULT_WRITER_MAX_SAMPLES, + DEFAULT_WRITER_MAX_SAMPLES, + DEFAULT_WRITER_MAX_SAMPLES, + ); + let sample = DDSData::new(SerializedPayload::new( + RepresentationIdentifier::CDR_LE, + vec![0; 8], + )); + assert!(matches!( + send_buffer.admit_blocking(WriteOptions::default(), sample, Some(Duration::ZERO)), + Admission::Admitted(_) + )); + let (status_sender, writer_status) = sync_status_channel::(16).unwrap(); + event_loop.add_local_writer(WriterIngredients { + guid: writer_guid, + send_buffer, + topic_name: "DCPSParticipantMessage".to_string(), + like_stateless: false, + qos_policies: Discovery::PARTICIPANT_MESSAGE_QOS, + status_sender, + security_plugins: None, + }); + + // The event loop learns about a remote participant. Returns whether the + // local writer is matched with that participant's + // BuiltinParticipantMessageReader then. + let discover = |event_loop: &mut DPEventLoop, data: &SpdpDiscoveredParticipantData| { + discovery_db_write(&event_loop.discovery_db).update_participant(data); + event_loop.update_participant(data.participant_guid.prefix); + let remote_reader = GUID::new( + data.participant_guid.prefix, + EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_READER, + ); + std::iter::from_fn(|| writer_status.try_recv().ok()).any(|status| { + matches!(status, DataWriterStatus::PublicationMatched { reader, current, .. } + if reader == remote_reader && current.count_change() == 1) + }) + }; + // Does the local writer's HEARTBEAT tick find a reader that it waits for? + let heartbeat_goes_on = |event_loop: &mut DPEventLoop| { + event_loop + .writers + .get_mut(&EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER) + .unwrap() + .handle_heartbeat_tick(false) + }; + + // A reliable BuiltinParticipantMessageReader that has acknowledged nothing + // keeps the HEARTBEAT going. + let reliable = remote_participant(GuidPrefix::new(b"reliable_rdr"), None); + assert!(discover(&mut event_loop, &reliable)); + assert!( + heartbeat_goes_on(&mut event_loop), + "no HEARTBEAT for a reliable reader that has acknowledged nothing" + ); + event_loop.remote_participant_lost(reliable.participant_guid.prefix); + assert!(!heartbeat_goes_on(&mut event_loop)); + + // A best-effort one does not. + let best_effort_reader = BuiltinEndpointQos::read_from_buffer_with_ctx( + Endianness::LittleEndian, + &BuiltinEndpointQos::BEST_EFFORT_PARTICIPANT_MESSAGE_DATA_READER.to_le_bytes(), + ) + .unwrap(); + let best_effort = + remote_participant(GuidPrefix::new(b"best_effort!"), Some(best_effort_reader)); + assert!(discover(&mut event_loop, &best_effort)); + assert!( + !heartbeat_goes_on(&mut event_loop), + "the writer waits for acknowledgments from a reader that was declared best-effort" + ); + } + // TODO: Rewrite / remove this test - all asserts in it use // DataReader::get_requested_deadline_missed_status which is // currently commented out