← Back to Pull requests

Discovery race condition mitigation #174

Merged
fgallegosalido merged 10 files (514 lines) on Mar 25, 2025 17:16 UTC
View on GitHub
fgallegosalido fgallegosalido commented on Mar 07, 2025 12:02 UTC
Author

This yet another attempt at merging a mitigation for the discovery race condition. The first attempt happened in https://github.com/ros2/rmw_connextdds/pull/132. This pull request has the same fix plus a couple of fixes and improvements.

Diff: 514 lines (+459 / -55) across 10 files Reviewer: lobolanja Review duration: 26233m
All Vouch review confidence checks passed
Residual risk 0.14 = Change Risk (0.54) × (1 − Review Confidence (0.74)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.54 · 514 lines diff across 10 files
0.54
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.51 · 5 substantive comment(s)
0.51
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
74%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.374
Real PR Conversation Timeline
48 items
fgallegosalido fgallegosalido commented on Mar 07, 2025 12:50 UTC
Author

@fujitatomoya Could you please run the CI for us? I don't have access to ROS 2 CI yet

fujitatomoya fujitatomoya commented on Mar 07, 2025 18:06 UTC
Contributor
fercsrti77 fercsrti77 commented on code on Mar 10, 2025 13:46 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/dds_api.hpp:1
@@ -292,4 +296,9 @@ rmw_connextdds_get_cft_filter_expression(
   rcutils_allocator_t * const allocator,
   rmw_subscription_content_filter_options_t * const options);
 
+rmw_ret_t
+rmw_connextdds_guid_to_instance_handle(
+  const struct DDS_GUID_t * const guid,
+  DDS_InstanceHandle_t * const instanceHandle);

instanceHandle should be instance_handle

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 13:47 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/log.hpp:1
@@ -45,7 +45,7 @@
     __VA_ARGS__);
 
 #define RMW_CONNEXT_LOG_INFO(msg_) \
-  RCUTILS_LOG_DEBUG_NAMED( \
+  RCUTILS_LOG_INFO_NAMED( \

should we file a bug about this issue to not lose track?

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 22:51 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -1020,6 +1022,97 @@ RMW_Connext_Publisher::qos(rmw_qos_profile_t * const qos)
   return rc;
 }
 
+std::chrono::microseconds
+RMW_Connext_Publisher::load_max_blocking_time() const
+{
+#if !RMW_CONNEXT_DDS_API_PRO_LEGACY
+  DDS_DataWriterQos dw_qos = DDS_DataWriterQos_INITIALIZER;
+#else
+  DDS_DataWriterQos dw_qos;
+  if (DDS_RETCODE_OK != DDS_DataWriterQos_initialize(&dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to initialize datawriter qos")
+    return std::chrono::microseconds(0);
+  }
+#endif /* !RMW_CONNEXT_DDS_API_PRO_LEGACY */
+
+  auto scope_exit_dw_qos_delete =
+    rcpputils::make_scope_exit(
+    [&dw_qos]()
+    {
+      if (DDS_RETCODE_OK != DDS_DataWriterQos_finalize(&dw_qos)) {
+        RMW_CONNEXT_LOG_ERROR_SET("failed to finalize DataWriterQoS")
+      }
+    });
+
+  if (DDS_DataWriter_get_qos(writer(), &dw_qos)) {

if (DDS_RETCODE_OK != DDS_DataWriter_get_qos(writer(), &dw_qos)) {

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 22:55 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/resource_limits.hpp:1
@@ -55,4 +55,13 @@
 #define RMW_CONNEXT_LIMIT_KEEP_ALL_SAMPLES              1000
 #endif /* RMW_CONNEXT_LIMIT_SAMPLES_MAX */
 
+#ifndef RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_DEFAULT

No need for this constant because its name is misleading. The constant is only used in load_max_blocking_time if there is an error getting the writer QoS (not expected) or when dw_qos.reliability.max_blocking_time is set to AUTO which is not allowed in Connext.

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 23:00 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -1020,6 +1022,97 @@ RMW_Connext_Publisher::qos(rmw_qos_profile_t * const qos)
   return rc;
 }
 
+std::chrono::microseconds
+RMW_Connext_Publisher::load_max_blocking_time() const
+{
+#if !RMW_CONNEXT_DDS_API_PRO_LEGACY
+  DDS_DataWriterQos dw_qos = DDS_DataWriterQos_INITIALIZER;
+#else
+  DDS_DataWriterQos dw_qos;
+  if (DDS_RETCODE_OK != DDS_DataWriterQos_initialize(&dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to initialize datawriter qos")
+    return std::chrono::microseconds(0);
+  }
+#endif /* !RMW_CONNEXT_DDS_API_PRO_LEGACY */
+
+  auto scope_exit_dw_qos_delete =
+    rcpputils::make_scope_exit(
+    [&dw_qos]()
+    {
+      if (DDS_RETCODE_OK != DDS_DataWriterQos_finalize(&dw_qos)) {
+        RMW_CONNEXT_LOG_ERROR_SET("failed to finalize DataWriterQoS")
+      }
+    });
+
+  if (DDS_DataWriter_get_qos(writer(), &dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to get datawriter qos")
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_DEFAULT);
+  }
+  if (DDS_Duration_is_auto(&dw_qos.reliability.max_blocking_time)) {

Remove this check.

dw_qos.reliability.max_blocking_time cannot have the AUTO value. The range is: [0,1 year] or DDS_DURATION_INFINITE

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 23:00 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -1020,6 +1022,97 @@ RMW_Connext_Publisher::qos(rmw_qos_profile_t * const qos)
   return rc;
 }
 
+std::chrono::microseconds
+RMW_Connext_Publisher::load_max_blocking_time() const
+{
+#if !RMW_CONNEXT_DDS_API_PRO_LEGACY
+  DDS_DataWriterQos dw_qos = DDS_DataWriterQos_INITIALIZER;
+#else
+  DDS_DataWriterQos dw_qos;
+  if (DDS_RETCODE_OK != DDS_DataWriterQos_initialize(&dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to initialize datawriter qos")
+    return std::chrono::microseconds(0);
+  }
+#endif /* !RMW_CONNEXT_DDS_API_PRO_LEGACY */
+
+  auto scope_exit_dw_qos_delete =
+    rcpputils::make_scope_exit(
+    [&dw_qos]()
+    {
+      if (DDS_RETCODE_OK != DDS_DataWriterQos_finalize(&dw_qos)) {
+        RMW_CONNEXT_LOG_ERROR_SET("failed to finalize DataWriterQoS")
+      }
+    });
+
+  if (DDS_DataWriter_get_qos(writer(), &dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to get datawriter qos")
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_DEFAULT);
+  }
+  if (DDS_Duration_is_auto(&dw_qos.reliability.max_blocking_time)) {
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_DEFAULT);
+  }
+  if (DDS_Duration_is_infinite(&dw_qos.reliability.max_blocking_time) ||
+    DDS_Duration_is_auto(&dw_qos.reliability.max_blocking_time))

Remove the DDS_Duration_is_auto check

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 23:01 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -1020,6 +1022,97 @@ RMW_Connext_Publisher::qos(rmw_qos_profile_t * const qos)
   return rc;
 }
 
+std::chrono::microseconds
+RMW_Connext_Publisher::load_max_blocking_time() const
+{
+#if !RMW_CONNEXT_DDS_API_PRO_LEGACY
+  DDS_DataWriterQos dw_qos = DDS_DataWriterQos_INITIALIZER;
+#else
+  DDS_DataWriterQos dw_qos;
+  if (DDS_RETCODE_OK != DDS_DataWriterQos_initialize(&dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to initialize datawriter qos")
+    return std::chrono::microseconds(0);
+  }
+#endif /* !RMW_CONNEXT_DDS_API_PRO_LEGACY */
+
+  auto scope_exit_dw_qos_delete =
+    rcpputils::make_scope_exit(
+    [&dw_qos]()
+    {
+      if (DDS_RETCODE_OK != DDS_DataWriterQos_finalize(&dw_qos)) {
+        RMW_CONNEXT_LOG_ERROR_SET("failed to finalize DataWriterQoS")
+      }
+    });
+
+  if (DDS_DataWriter_get_qos(writer(), &dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to get datawriter qos")
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_DEFAULT);
+  }
+  if (DDS_Duration_is_auto(&dw_qos.reliability.max_blocking_time)) {
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_DEFAULT);
+  }
+  if (DDS_Duration_is_infinite(&dw_qos.reliability.max_blocking_time) ||
+    DDS_Duration_is_auto(&dw_qos.reliability.max_blocking_time))
+  {
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_INFINITE);
+  }
+  if (DDS_Duration_is_zero(&dw_qos.reliability.max_blocking_time)) {
+    return std::chrono::microseconds(0);
+  }
+  std::chrono::microseconds max_blocking_time =
+    std::chrono::duration_cast<std::chrono::milliseconds>(

std::chrono::duration_cast<std::chrono::microseconds >

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 23:02 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -1020,6 +1022,97 @@ RMW_Connext_Publisher::qos(rmw_qos_profile_t * const qos)
   return rc;
 }
 
+std::chrono::microseconds
+RMW_Connext_Publisher::load_max_blocking_time() const
+{
+#if !RMW_CONNEXT_DDS_API_PRO_LEGACY
+  DDS_DataWriterQos dw_qos = DDS_DataWriterQos_INITIALIZER;
+#else
+  DDS_DataWriterQos dw_qos;
+  if (DDS_RETCODE_OK != DDS_DataWriterQos_initialize(&dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to initialize datawriter qos")
+    return std::chrono::microseconds(0);
+  }
+#endif /* !RMW_CONNEXT_DDS_API_PRO_LEGACY */
+
+  auto scope_exit_dw_qos_delete =
+    rcpputils::make_scope_exit(
+    [&dw_qos]()
+    {
+      if (DDS_RETCODE_OK != DDS_DataWriterQos_finalize(&dw_qos)) {
+        RMW_CONNEXT_LOG_ERROR_SET("failed to finalize DataWriterQoS")
+      }
+    });
+
+  if (DDS_DataWriter_get_qos(writer(), &dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to get datawriter qos")
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_DEFAULT);

return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_INFINITE);

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 23:45 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -217,6 +242,12 @@ class RMW_Connext_Publisher
     DDS_SampleIdentity_t * const sample_identity,
     DDS_SampleIdentity_t * const related_sample_identity);
 
+  rmw_ret_t
+  wait_for_subscription(
+    rmw_gid_t & reader_gid,
+    bool & unmatched,

Rename unmatched with unknown. The unmatched term is misleading. See my previous function comment.

fercsrti77 fercsrti77 commented on code on Mar 10, 2025 23:55 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -217,6 +242,12 @@ class RMW_Connext_Publisher
     DDS_SampleIdentity_t * const sample_identity,
     DDS_SampleIdentity_t * const related_sample_identity);
 
+  rmw_ret_t

Add comment explaining the function

// Waits for the `RMW_Connext_Publisher`'s DataWriter to match a known
// DataReader with GID `reader_gid`.
//
// A DataReader is considered known if its GID is present in the 
// `known_endpoints` map.
//
// If this API is called with a `reader_gid` that is not in `known_endpoints`,
// it returns `RMW_RET_OK` with `unknown` set to `true`.
//
// An `RMW_Connext_Service` (using Extended mapping) uses this API to wait for 
// an `RMW_Connext_Client`'s DataReader, identified by `reader_gid`, to match 
// its DataWriter.
//
// An `RMW_Connext_Service` adds a DataReader's GID to `known_endpoints` when 
// it receives a request from its own DataReader.

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 00:16 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -234,19 +265,82 @@ class RMW_Connext_Publisher
     return DDS_Publisher_get_participant(pub);
   }
 
+  void
+  push_related_endpoints(const rmw_gid_t & endpoint, const rmw_gid_t & related)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(endpoint), related);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(related), endpoint);
+  }
+
+  void
+  on_publication_matched(
+    const DDS_PublicationMatchedStatus * const status)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    DDS_ReturnCode_t dds_rc =
+      DDS_DataWriter_get_matched_subscriptions(writer(), &matched_subscriptions);
+    if (DDS_RETCODE_OK != dds_rc) {
+      RMW_CONNEXT_LOG_ERROR_A_SET("failed to list matched subscriptions: dds_rc=%d", dds_rc)
+    }
+    if (status->current_count_change < 0) {
+      rmw_gid_t unmatched_gid;
+      rmw_connextdds_ih_to_gid(status->last_subscription_handle, unmatched_gid);
+      pop_related_endpoints(unmatched_gid);
+    }
+    matched_cv.notify_all();
+  }
+
+  void
+  on_related_subscription_matched(

Add comment

  // This method is called by a RMW_Connext_Service when a RMW_Connext_Client's
  // DataWriter is matched/unmatched with the Service's DataReader 

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 00:21 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -234,19 +265,82 @@ class RMW_Connext_Publisher
     return DDS_Publisher_get_participant(pub);
   }
 
+  void
+  push_related_endpoints(const rmw_gid_t & endpoint, const rmw_gid_t & related)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(endpoint), related);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(related), endpoint);
+  }
+
+  void
+  on_publication_matched(
+    const DDS_PublicationMatchedStatus * const status)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    DDS_ReturnCode_t dds_rc =
+      DDS_DataWriter_get_matched_subscriptions(writer(), &matched_subscriptions);
+    if (DDS_RETCODE_OK != dds_rc) {
+      RMW_CONNEXT_LOG_ERROR_A_SET("failed to list matched subscriptions: dds_rc=%d", dds_rc)
+    }
+    if (status->current_count_change < 0) {
+      rmw_gid_t unmatched_gid;
+      rmw_connextdds_ih_to_gid(status->last_subscription_handle, unmatched_gid);
+      pop_related_endpoints(unmatched_gid);
+    }
+    matched_cv.notify_all();
+  }
+
+  void
+  on_related_subscription_matched(
+    RMW_Connext_Subscriber * const sub,
+    const DDS_SubscriptionMatchedStatus * const status)
+  {
+    UNUSED_ARG(sub);
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    if (status->current_count_change < 0) {
+      rmw_gid_t unmatched_gid;
+      rmw_connextdds_ih_to_gid(status->last_publication_handle, unmatched_gid);
+      pop_related_endpoints(unmatched_gid);
+    }
+    matched_cv.notify_all();
+  }
+
+  rmw_context_impl_t * const ctx;
+
+  ~RMW_Connext_Publisher();
+
 private:
-  rmw_context_impl_t * ctx;
   DDS_DataWriter * dds_writer;
   RMW_Connext_MessageTypeSupport * type_support;
   const bool created_topic;
   rmw_gid_t ros_gid;
   RMW_Connext_PublisherStatusCondition status_condition;
+  std::mutex matched_mutex;
+  std::condition_variable matched_cv;
+  std::chrono::microseconds max_blocking_time;
+  std::map<RMW_Connext_OrderedGid, rmw_gid_t> known_endpoints;

Add comment

  // Map of endpoints to related endpoints associated with a 
  // RMW_Connext_Publisher.
  //
  // In a RMW_Connext_Service's RMW_Connext_Publisher, this map
  // contains two pairs for each matched RMW_Connext_Client:
  // (RMW_Connext_Client's DataWriter, RMW_Connext_Client's DataReader)
  // (RMW_Connext_Client's DataReader, RMW_Connext_Client's DataWriter)

fercsrti77 fercsrti77 requested changes on Mar 11, 2025 00:33 UTC
Changes Requested Reviewer

Requested changes.

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 13:19 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/type_support.hpp:1
@@ -45,6 +45,7 @@ struct RMW_Connext_RequestReplyMessage
 {
   bool request;
   rmw_gid_t gid;
+  rmw_gid_t writer_gid;

Add relevant comments

// This type is internal to the implementation and it is not sent over the wire
struct RMW_Connext_RequestReplyMessage
{
  bool request;
  // In basic mapping, this is the GID of the client's DataWriter
  // In extended mapping, this is the GID of the client's DataReader
  rmw_gid_t gid;
  // This is the GID of the writer that sent the request
  rmw_gid_t writer_gid;
  int64_t sn;
  void * payload;
};

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 13:31 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -667,6 +668,11 @@ RMW_Connext_Publisher::RMW_Connext_Publisher(
   }
 }
 
+RMW_Connext_Publisher::~RMW_Connext_Publisher()
+{
+  DDS_InstanceHandleSeq_finalize(&matched_subscriptions);

Print an error message if the call to this function returns DDS_BOOLEAN_FALSE

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 13:33 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -1667,17 +1764,21 @@ RMW_Connext_Subscriber::requestreply_header_from_dds(
 {
   const struct DDS_GUID_t * src_guid = nullptr;
   const struct DDS_SequenceNumber_t * src_sn = nullptr;
+  const struct DDS_GUID_t * writer_guid = nullptr;
 
   if (rr_msg->request) {
-    src_guid = &sample_identity->writer_guid;
+    src_guid = &related_sample_identity->writer_guid;

Add comment

    // For extended request reply mapping this is the Client's DataReader GUID 
    // which we are sending as part of the related_sample_identity
    src_guid = &related_sample_identity->writer_guid;

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 13:55 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -234,19 +265,82 @@ class RMW_Connext_Publisher
     return DDS_Publisher_get_participant(pub);
   }
 
+  void
+  push_related_endpoints(const rmw_gid_t & endpoint, const rmw_gid_t & related)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(endpoint), related);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(related), endpoint);
+  }
+
+  void
+  on_publication_matched(
+    const DDS_PublicationMatchedStatus * const status)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);

We only want to run this method if we are dealing with a Service's DataWriter. Add this guard:

// This callback is only used by the RMW_Connext_Service's RMW_Connext_Publisher
if (this->type_support->message_type() != RMW_CONNEXT_MESSAGE_REPLY) {
  return;
}

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 13:56 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -234,19 +265,82 @@ class RMW_Connext_Publisher
     return DDS_Publisher_get_participant(pub);
   }
 
+  void
+  push_related_endpoints(const rmw_gid_t & endpoint, const rmw_gid_t & related)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(endpoint), related);
+    known_endpoints.emplace(RMW_Connext_OrderedGid(related), endpoint);
+  }
+
+  void
+  on_publication_matched(
+    const DDS_PublicationMatchedStatus * const status)
+  {
+    std::lock_guard<std::mutex> lock(matched_mutex);
+    DDS_ReturnCode_t dds_rc =
+      DDS_DataWriter_get_matched_subscriptions(writer(), &matched_subscriptions);
+    if (DDS_RETCODE_OK != dds_rc) {
+      RMW_CONNEXT_LOG_ERROR_A_SET("failed to list matched subscriptions: dds_rc=%d", dds_rc)
+    }
+    if (status->current_count_change < 0) {
+      rmw_gid_t unmatched_gid;
+      rmw_connextdds_ih_to_gid(status->last_subscription_handle, unmatched_gid);
+      pop_related_endpoints(unmatched_gid);
+    }
+    matched_cv.notify_all();
+  }
+
+  void
+  on_related_subscription_matched(

// This callback is only used by the RMW_Connext_Service's RMW_Connext_Publisher
if (this->type_support->message_type() != RMW_CONNEXT_MESSAGE_REPLY) {
return;
}

fercsrti77 fercsrti77 requested changes on Mar 11, 2025 15:26 UTC
Changes Requested Reviewer

Requested changes.

fercsrti77 fercsrti77 commented on code on Mar 11, 2025 15:31 UTC
Code Review
rmw_connextdds_common/src/common/rmw_context.cpp:1

Can you merge rolling into this branch. There have been a few changes in rmw_connext.cpp

fercsrti77 fercsrti77 requested changes on Mar 11, 2025 15:32 UTC
Changes Requested Reviewer

Requested changes.

fgallegosalido fgallegosalido commented on code on Mar 11, 2025 17:46 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/log.hpp:1
@@ -45,7 +45,7 @@
     __VA_ARGS__);
 
 #define RMW_CONNEXT_LOG_INFO(msg_) \
-  RCUTILS_LOG_DEBUG_NAMED( \
+  RCUTILS_LOG_INFO_NAMED( \
fgallegosalido fgallegosalido commented on Mar 13, 2025 15:46 UTC
Author

@fujitatomoya can you run the CI please? Hopefully the changes fix the instabilities.

fujitatomoya fujitatomoya commented on Mar 13, 2025 17:40 UTC
Contributor
fercsrti77 fercsrti77 commented on code on Mar 13, 2025 18:53 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -217,6 +242,23 @@ class RMW_Connext_Publisher
     DDS_SampleIdentity_t * const sample_identity,
     DDS_SampleIdentity_t * const related_sample_identity);
 
+  // Waits for the `RMW_Connext_Publisher`'s DataWriter to match a known

No need to use `` in these comments

fercsrti77 fercsrti77 commented on code on Mar 13, 2025 19:00 UTC
Code Review
rmw_connextdds_common/include/rmw_connextdds/rmw_impl.hpp:1
@@ -217,6 +242,23 @@ class RMW_Connext_Publisher
     DDS_SampleIdentity_t * const sample_identity,
     DDS_SampleIdentity_t * const related_sample_identity);
 
+  // Waits for the `RMW_Connext_Publisher`'s DataWriter to match a known
+  // DataReader associated with the `related_writer_gid`.
+  //
+  // A DataReader is considered known if the associated DataWriter identified by
+  // the `related_writer_gid` is in the `known_endpoints` map.
+  //
+  // If this API is called with a `related_writer_gid` that is not in the
+  // `known_endpoints`, it returns `RMW_RET_OK` with `unknown` set to `true`.
+  //
+  // An `RMW_Connext_Service` (using Extended mapping) uses this API to wait for
+  // an `RMW_Connext_Client`'s DataReader, associated with the DataWriter identified by `related_writer_gid`, to match
+  // the Service's DataWriter.
+  //
+  // An `RMW_Connext_Service` adds the `related_writer_gid` to `known_endpoints` when
+  // it receives a request from its own DataReader.
+  rmw_ret_t wait_for_subscription(rmw_gid_t &related_writer_gid, bool &unknown);

Consider changing this name to wait_for_client_subscription as the semantic is quiet specific to the Client/Service communication pattern. New prototype:

  // When invoked in a Service's RMW_Connext_Publisher, this API waits for 
  // the RMW_Connext_Publisher's DataWriter to match the Client's DataReader.
  // associated with the Client's DataWriter identified by `client_writer_gid`.
  // 
  // If this API is not invoked in a Service's RMW_Connext_Publisher, it returns
  // `RMW_RET_ERROR`.
  //
  // If the `client_writer_gid` is not in the `known_endpoints`, it returns
  // `RMW_RET_OK` with `unknown` set to `true`.
  //
  // Known endpoints are added to the `known_endpoints` map when a Service's
  // DataReader receives a request from a Client's DataWriter using the API
  // push_related_endpoints
  rmw_ret_t wait_for_client_subscription(rmw_gid_t &client_writer_gid, bool &unknown);

fercsrti77 fercsrti77 commented on code on Mar 13, 2025 20:41 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -1020,6 +1024,93 @@ RMW_Connext_Publisher::qos(rmw_qos_profile_t * const qos)
   return rc;
 }
 
+std::chrono::microseconds
+RMW_Connext_Publisher::load_max_blocking_time() const
+{
+#if !RMW_CONNEXT_DDS_API_PRO_LEGACY
+  DDS_DataWriterQos dw_qos = DDS_DataWriterQos_INITIALIZER;
+#else
+  DDS_DataWriterQos dw_qos;
+  if (DDS_RETCODE_OK != DDS_DataWriterQos_initialize(&dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to initialize datawriter qos")
+    return std::chrono::microseconds(0);
+  }
+#endif /* !RMW_CONNEXT_DDS_API_PRO_LEGACY */
+
+  auto scope_exit_dw_qos_delete =
+    rcpputils::make_scope_exit(
+    [&dw_qos]()
+    {
+      if (DDS_RETCODE_OK != DDS_DataWriterQos_finalize(&dw_qos)) {
+        RMW_CONNEXT_LOG_ERROR_SET("failed to finalize DataWriterQoS")
+      }
+    });
+
+  if (DDS_RETCODE_OK != DDS_DataWriter_get_qos(writer(), &dw_qos)) {
+    RMW_CONNEXT_LOG_ERROR_SET("failed to get datawriter qos")
+    return std::chrono::microseconds(
+        RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_INFINITE);
+  }
+  if (DDS_Duration_is_infinite(&dw_qos.reliability.max_blocking_time))
+  {
+    return std::chrono::microseconds(RMW_CONNEXT_LIMIT_DEFAULT_BLOCKING_TIME_INFINITE);
+  }
+  if (DDS_Duration_is_zero(&dw_qos.reliability.max_blocking_time)) {
+    return std::chrono::microseconds(0);
+  }
+  std::chrono::microseconds max_blocking_time =
+    std::chrono::duration_cast<std::chrono::microseconds>(
+    std::chrono::seconds(dw_qos.reliability.max_blocking_time.sec) +
+    std::chrono::nanoseconds(dw_qos.reliability.max_blocking_time.nanosec));
+
+  return max_blocking_time;
+}
+
+rmw_ret_t
+RMW_Connext_Publisher::wait_for_subscription(
+  rmw_gid_t & client_writer_gid,
+  bool & unknown)
+{
+  unknown = false;
+
+  struct DDS_GUID_t reader_guid = DDS_GUID_INITIALIZER;
+  rmw_ret_t rc = RMW_RET_ERROR;
+
+  std::unique_lock<std::mutex> lock(matched_mutex);

return an error in the RMW_Connext_Publisher is not a Service's RMW_Connext_Publisher

if (this->type_support->message_type() != RMW_CONNEXT_MESSAGE_REPLY) {
      return RMW_RET_ERROR;
}

fercsrti77 fercsrti77 requested changes on Mar 13, 2025 21:05 UTC
Changes Requested Reviewer

Requested changes.

fgallegosalido fgallegosalido commented on Mar 14, 2025 11:48 UTC
Author

I think the pull request is ready to be merged. @fujitatomoya can you run the CI for us?

lobolanja lobolanja approved these changes on Mar 14, 2025 12:36 UTC
Approved Reviewer

@fgallegosalido, As long as the tests pass, I’m fine with merging this Pull Request.

fujitatomoya fujitatomoya commented on Mar 18, 2025 04:52 UTC
Contributor
fujitatomoya fujitatomoya commented on code on Mar 18, 2025 06:01 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -3078,6 +3201,62 @@ RMW_Connext_Service::send_response(
     request_id->sequence_number,
     dds_time_to_u64(&write_params.timestamp));
 
+  /* (asorbini) The following logic tries to partially work around some race conditions that exists
+     in the way request/reply interactions between clients and services are mapped to DDS topics
+     and endpoints.
+
+     It is possible for the service's request DataReader to receive a request from the client,
+     handle it, and try to write the response before the service's reply DataWriter has actually
+     matched the client's reply DataReader. If this is the case, the response will be lost
+     (unless the endpoints have been configured with "transient local" or higher Durability QoS,
+     but this is not the default configuration, and it is unlikely to be used in practice...).
+     The data race and loss of reply message may also occur in the case of an
+     "asymmetric discovery" between the client's reply DataReader and the service's reply
+     DataWriter, where the latter has discovered and matched the former, but not viceversa.
+
+     The only way to fully resolve these data races is to have the underlying implementation force
+     a "match ordering" when dealing with pairs of endpoints used for RPC exchanges. The solution
+     is described by the OMG DDS-RPC 1.0 specification, section 7.6.2 (Enhanced Service Mapping).
+     It requires the DDS implementation to prevent the DataWriter from matching its remote
+     counterpart before the associated DataReader has matched. This behavior must be enforced
+     by both service and client for all data races to be resolved.
+
+     This partial workaround relies on (ab)using the "related sample identity" field to communicate
+     the client's reply DataReader's GUID to the service. Normally, this field should would be
+     used to communicate the client's request DataWriter's GUID.
+
+     When the service detects the presence of an unusual DataReader GUID, it will enter a
+     (time-bounded) loop to query the reply DataWriter's list of matched subscriptions, and wait
+     for the GUID to appear.
+
+     If the reader is not matched within the timeout (HistoryQosPolicy::max_blocking_time), the
+     service will return RMW_RET_TIMEOUT to the upper layers. Otherwise, the response is written
+     out as normal.
+
+     It is still possible for the response to be lost by the client, in case of an asymmetric
+     discovery, but this issue partially mitigated by the implementation of
+     Client::is_service_available.
+  */
+  if (ctx->request_reply_mapping == RMW_Connext_RequestReplyMapping::Extended) {
+    struct DDS_GUID_t src_guid = DDS_GUID_INITIALIZER;
+    rmw_ret_t rc = RMW_RET_ERROR;
+    rc = rmw_connextdds_gid_to_guid(rr_msg.gid, src_guid);
+    if (RMW_RET_OK != rc) {
+      return rc;
+    }
+    DDS_RTPS_GUID_t * const rtps_guid = DDS_GUID_as_rtps_guid(&src_guid);
+    if (rtps_guid->entityId.entityKind & 0x03) {
+      bool unknown = false;
+      rmw_gid_t client_writer_gid;
+      client_writer_gid.implementation_identifier = RMW_CONNEXTDDS_ID;
+      std::copy_n(request_id->writer_guid, RMW_GID_STORAGE_SIZE, client_writer_gid.data);
+      rc = reply_pub->wait_for_client_subscription(client_writer_gid, unknown);
+      if (RMW_RET_OK != rc || unknown) {
+        return rc;
+      }

i think unknown need to be handled as warning case, because it is not expected to happen usually? returning success with unknown=true, it just looks like send_response successfully writes the message to the associated data reader from the caller. but that is not what happened here.

fujitatomoya fujitatomoya commented on Mar 18, 2025 07:09 UTC
Reviewer

these changes aim to enhance the functionality and reliability of the RMW_Connext_Publisher (service server response sender) and RMW_Connext_Subscriber (service client response reader), particularly in managing endpoint relationships and synchronization for the service. IMO, this really makes sense and probably mitigate the racy condition to miss the response message between service server and clients.

just a minor comment that i would like to confirm.

lobolanja lobolanja commented on code on Mar 19, 2025 23:13 UTC
Code Review
rmw_connextdds_common/src/common/rmw_impl.cpp:1
@@ -3078,6 +3201,62 @@ RMW_Connext_Service::send_response(
     request_id->sequence_number,
     dds_time_to_u64(&write_params.timestamp));
 
+  /* (asorbini) The following logic tries to partially work around some race conditions that exists
+     in the way request/reply interactions between clients and services are mapped to DDS topics
+     and endpoints.
+
+     It is possible for the service's request DataReader to receive a request from the client,
+     handle it, and try to write the response before the service's reply DataWriter has actually
+     matched the client's reply DataReader. If this is the case, the response will be lost
+     (unless the endpoints have been configured with "transient local" or higher Durability QoS,
+     but this is not the default configuration, and it is unlikely to be used in practice...).
+     The data race and loss of reply message may also occur in the case of an
+     "asymmetric discovery" between the client's reply DataReader and the service's reply
+     DataWriter, where the latter has discovered and matched the former, but not viceversa.
+
+     The only way to fully resolve these data races is to have the underlying implementation force
+     a "match ordering" when dealing with pairs of endpoints used for RPC exchanges. The solution
+     is described by the OMG DDS-RPC 1.0 specification, section 7.6.2 (Enhanced Service Mapping).
+     It requires the DDS implementation to prevent the DataWriter from matching its remote
+     counterpart before the associated DataReader has matched. This behavior must be enforced
+     by both service and client for all data races to be resolved.
+
+     This partial workaround relies on (ab)using the "related sample identity" field to communicate
+     the client's reply DataReader's GUID to the service. Normally, this field should would be
+     used to communicate the client's request DataWriter's GUID.
+
+     When the service detects the presence of an unusual DataReader GUID, it will enter a
+     (time-bounded) loop to query the reply DataWriter's list of matched subscriptions, and wait
+     for the GUID to appear.
+
+     If the reader is not matched within the timeout (HistoryQosPolicy::max_blocking_time), the
+     service will return RMW_RET_TIMEOUT to the upper layers. Otherwise, the response is written
+     out as normal.
+
+     It is still possible for the response to be lost by the client, in case of an asymmetric
+     discovery, but this issue partially mitigated by the implementation of
+     Client::is_service_available.
+  */
+  if (ctx->request_reply_mapping == RMW_Connext_RequestReplyMapping::Extended) {
+    struct DDS_GUID_t src_guid = DDS_GUID_INITIALIZER;
+    rmw_ret_t rc = RMW_RET_ERROR;
+    rc = rmw_connextdds_gid_to_guid(rr_msg.gid, src_guid);
+    if (RMW_RET_OK != rc) {
+      return rc;
+    }
+    DDS_RTPS_GUID_t * const rtps_guid = DDS_GUID_as_rtps_guid(&src_guid);
+    if (rtps_guid->entityId.entityKind & 0x03) {
+      bool unknown = false;
+      rmw_gid_t client_writer_gid;
+      client_writer_gid.implementation_identifier = RMW_CONNEXTDDS_ID;
+      std::copy_n(request_id->writer_guid, RMW_GID_STORAGE_SIZE, client_writer_gid.data);
+      rc = reply_pub->wait_for_client_subscription(client_writer_gid, unknown);
+      if (RMW_RET_OK != rc || unknown) {
+        return rc;
+      }

@fujitatomoya

We have reviewed your comment and confirmed that maintaining compatibility with older clients or clients from other vendors could trigger getting “unknown” from wait_for_client_subscription.

To address this, we have modified the solution and added more comments to clarify the different situations that may occur. Additionally, we have included a warning message to notify the user. you can check the changes in this commit: https://github.com/ros2/rmw_connextdds/pull/174/commits/0e02fc576f4b0cb3a6df5bd722eca595e810a434

Thanks for flagging that! 🙂

lobolanja lobolanja commented on Mar 19, 2025 23:18 UTC
Contributor

@fujitatomoya

Could you help with another CI validation run? I have updated the branch to the latest rolling state since the last run failed due to a build issue with a package unrelated to rmw_connext.

fujitatomoya fujitatomoya approved these changes on Mar 20, 2025 16:22 UTC
Approved Reviewer

Approved these changes.

fujitatomoya fujitatomoya commented on Mar 20, 2025 16:23 UTC
Contributor
lobolanja lobolanja commented on Mar 21, 2025 21:56 UTC
Contributor

@fujitatomoya

The Linux-rhel build has failed, but it appears to be unrelated to the changes in this PR. Are we allowed to merge it, or should we look into the issue more deeply?

In any case, I’ve also fixed the uncrustify formatting issues. Do we need to rerun the Linux and Windows builds now?

Lastly, thanks a lot for running the CI for us! Do you know who we should contact to get access to run the builds ourselves in the future?

fujitatomoya fujitatomoya commented on Mar 23, 2025 23:29 UTC
Contributor

@lobolanja i will restart the CI.

fujitatomoya fujitatomoya commented on Mar 23, 2025 23:29 UTC
Contributor
fercsrti77 fercsrti77 approved these changes on Mar 24, 2025 17:41 UTC
Approved Reviewer

Approved these changes.

fgallegosalido fgallegosalido commented on Mar 24, 2025 17:43 UTC
Author

@fujitatomoya the Windows build was aborted, but the test issues that affected previous builds seem to be finished as per the Linux build. Are we okay to merge?

fujitatomoya fujitatomoya commented on Mar 24, 2025 19:51 UTC
Contributor

@fgallegosalido test was aborted, so not completed. i just started the new CI for windows.

  • Windows ![Build Status](http://ci.ros2.org/job/ci_windows/23727/)
  • when this comes green, i believe that it is good to go.

    fgallegosalido fgallegosalido commented on Mar 25, 2025 09:49 UTC
    Author

    @fujitatomoya a couple of tests failed in Windows, but they don't seem related to the changes in this pr. Is it okay to merge now?

    Reviewers
    Assignees
    fgallegosalido fgallegosalido
    Labels
    risk: low confidence: 74.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 53.5% (MEDIUM) · Higher = larger blast radius
    53.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 74.3% · Lower = rushed or shallow review
    74.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 13.8% · Safe to merge
    13.8%