Skip to content

Commit 767d440

Browse files
committed
Fix for rosbag2_transport::Recorder failures due to unhandled exceptions
Signed-off-by: Michael Orlov <[email protected]>
1 parent e552f15 commit 767d440

File tree

2 files changed

+91
-49
lines changed

2 files changed

+91
-49
lines changed

rosbag2_transport/src/rosbag2_transport/recorder.cpp

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,13 @@ void RecorderImpl::event_publisher_thread_main()
317317
auto message = rosbag2_interfaces::msg::WriteSplitEvent();
318318
message.closed_file = bag_split_info_.closed_file;
319319
message.opened_file = bag_split_info_.opened_file;
320-
split_event_pub_->publish(message);
320+
try {
321+
split_event_pub_->publish(message);
322+
} catch (const std::exception & e) {
323+
RCLCPP_ERROR_STREAM(
324+
node->get_logger(),
325+
"Failed to publish message on '/events/write_split' topic. \nError: " << e.what());
326+
}
321327
}
322328

323329
should_exit = event_publisher_thread_should_exit_;
@@ -365,8 +371,7 @@ bool RecorderImpl::is_paused()
365371
void RecorderImpl::topics_discovery()
366372
{
367373
while (rclcpp::ok() && stop_discovery_ == false) {
368-
auto topics_to_subscribe =
369-
get_requested_or_available_topics();
374+
auto topics_to_subscribe = get_requested_or_available_topics();
370375
for (const auto & topic_and_type : topics_to_subscribe) {
371376
warn_if_new_qos_for_subscribed_topic(topic_and_type.first);
372377
}
@@ -386,7 +391,13 @@ void RecorderImpl::topics_discovery()
386391
std::unordered_map<std::string, std::string>
387392
RecorderImpl::get_requested_or_available_topics()
388393
{
389-
auto all_topics_and_types = node->get_topic_names_and_types();
394+
std::map<std::string, std::vector<std::string>> all_topics_and_types;
395+
try {
396+
all_topics_and_types = node->get_topic_names_and_types();
397+
} catch (const std::exception & e) {
398+
RCLCPP_ERROR_STREAM(
399+
node->get_logger(), "Failed to get topic names and types from node \nError: " << e.what());
400+
}
390401
return topic_filter_->filter_topics(all_topics_and_types);
391402
}
392403

@@ -407,7 +418,16 @@ void RecorderImpl::subscribe_topics(
407418
const std::unordered_map<std::string, std::string> & topics_and_types)
408419
{
409420
for (const auto & topic_with_type : topics_and_types) {
410-
auto endpoint_infos = node->get_publishers_info_by_topic(topic_with_type.first);
421+
std::vector<rclcpp::TopicEndpointInfo> endpoint_infos;
422+
try {
423+
endpoint_infos = node->get_publishers_info_by_topic(topic_with_type.first);
424+
} catch (const std::exception & e) {
425+
RCLCPP_ERROR_STREAM(
426+
node->get_logger(), "Failed to get publishers info for '" << topic_with_type.first <<
427+
"' topic. \nError: " << e.what());
428+
continue;
429+
}
430+
411431
subscribe_topic(
412432
{
413433
topic_with_type.first,
@@ -427,13 +447,19 @@ void RecorderImpl::subscribe_topic(const rosbag2_storage::TopicMetadata & topic)
427447
writer_->create_topic(topic);
428448

429449
Rosbag2QoS subscription_qos{subscription_qos_for_topic(topic.name)};
430-
auto subscription = create_subscription(topic.name, topic.type, subscription_qos);
431-
if (subscription) {
432-
subscriptions_.insert({topic.name, subscription});
433-
RCLCPP_INFO_STREAM(
434-
node->get_logger(),
435-
"Subscribed to topic '" << topic.name << "'");
436-
} else {
450+
try {
451+
auto subscription = create_subscription(topic.name, topic.type, subscription_qos);
452+
if (subscription) {
453+
subscriptions_.insert({topic.name, subscription});
454+
RCLCPP_INFO_STREAM(node->get_logger(), "Subscribed to topic '" << topic.name << "'");
455+
} else {
456+
writer_->remove_topic(topic);
457+
subscriptions_.erase(topic.name);
458+
}
459+
} catch (const std::exception & e) {
460+
RCLCPP_ERROR_STREAM(
461+
node->get_logger(), "Failed to create subscription for '" << topic.name <<
462+
"' topic. \nError: " << e.what());
437463
writer_->remove_topic(topic);
438464
subscriptions_.erase(topic.name);
439465
}
@@ -545,7 +571,16 @@ void RecorderImpl::warn_if_new_qos_for_subscribed_topic(const std::string & topi
545571
}
546572
const auto actual_qos = existing_subscription->second->get_actual_qos();
547573
const auto & used_profile = actual_qos.get_rmw_qos_profile();
548-
auto publishers_info = node->get_publishers_info_by_topic(topic_name);
574+
575+
std::vector<rclcpp::TopicEndpointInfo> publishers_info;
576+
try {
577+
publishers_info = node->get_publishers_info_by_topic(topic_name);
578+
} catch (const std::exception & e) {
579+
RCLCPP_ERROR_STREAM(
580+
node->get_logger(), "Failed to get publishers info for '" << topic_name <<
581+
"' topic \nError: " << e.what());
582+
}
583+
549584
for (const auto & info : publishers_info) {
550585
auto new_profile = info.qos_profile().get_rmw_qos_profile();
551586
bool incompatible_reliability =

rosbag2_transport/src/rosbag2_transport/topic_filter.cpp

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -117,49 +117,56 @@ std::unordered_map<std::string, std::string> TopicFilter::filter_topics(
117117
bool TopicFilter::take_topic(
118118
const std::string & topic_name, const std::vector<std::string> & topic_types)
119119
{
120-
if (!has_single_type(topic_name, topic_types)) {
121-
return false;
122-
}
120+
try {
121+
if (!has_single_type(topic_name, topic_types)) {
122+
return false;
123+
}
123124

124-
const std::string & topic_type = topic_types[0];
125-
if (!allow_unknown_types_ && !type_is_known(topic_name, topic_type)) {
126-
return false;
127-
}
125+
const std::string & topic_type = topic_types[0];
126+
if (!allow_unknown_types_ && !type_is_known(topic_name, topic_type)) {
127+
return false;
128+
}
128129

129-
if (!record_options_.include_hidden_topics && topic_is_hidden(topic_name)) {
130-
RCUTILS_LOG_WARN_ONCE_NAMED(
131-
ROSBAG2_TRANSPORT_PACKAGE_NAME,
132-
"Hidden topics are not recorded. Enable them with --include-hidden-topics");
133-
return false;
134-
}
130+
if (!record_options_.include_hidden_topics && topic_is_hidden(topic_name)) {
131+
RCUTILS_LOG_WARN_ONCE_NAMED(
132+
ROSBAG2_TRANSPORT_PACKAGE_NAME,
133+
"Hidden topics are not recorded. Enable them with --include-hidden-topics");
134+
return false;
135+
}
135136

136-
if (!record_options_.include_unpublished_topics && node_graph_ &&
137-
topic_is_unpublished(topic_name, *node_graph_))
138-
{
139-
return false;
140-
}
137+
if (!record_options_.include_unpublished_topics && node_graph_ &&
138+
topic_is_unpublished(topic_name, *node_graph_))
139+
{
140+
return false;
141+
}
141142

142-
if (record_options_.ignore_leaf_topics && node_graph_ &&
143-
is_leaf_topic(topic_name, *node_graph_))
144-
{
145-
return false;
146-
}
143+
if (record_options_.ignore_leaf_topics && node_graph_ &&
144+
is_leaf_topic(topic_name, *node_graph_))
145+
{
146+
return false;
147+
}
147148

148-
if (!record_options_.topics.empty() && !topic_in_list(topic_name, record_options_.topics)) {
149-
return false;
150-
}
149+
if (!record_options_.topics.empty() && !topic_in_list(topic_name, record_options_.topics)) {
150+
return false;
151+
}
151152

152-
std::regex exclude_regex(record_options_.exclude);
153-
if (!record_options_.exclude.empty() && std::regex_search(topic_name, exclude_regex)) {
154-
return false;
155-
}
153+
std::regex exclude_regex(record_options_.exclude);
154+
if (!record_options_.exclude.empty() && std::regex_search(topic_name, exclude_regex)) {
155+
return false;
156+
}
156157

157-
std::regex include_regex(record_options_.regex);
158-
if (
159-
!record_options_.all && // All takes precedence over regex
160-
!record_options_.regex.empty() && // empty regex matches nothing, but should be ignored
161-
!std::regex_search(topic_name, include_regex))
162-
{
158+
std::regex include_regex(record_options_.regex);
159+
if (
160+
!record_options_.all && // All takes precedence over regex
161+
!record_options_.regex.empty() && // empty regex matches nothing, but should be ignored
162+
!std::regex_search(topic_name, include_regex))
163+
{
164+
return false;
165+
}
166+
} catch (const std::exception & e) {
167+
ROSBAG2_TRANSPORT_LOG_ERROR_STREAM(
168+
"Failed to get information about topic '" << topic_name <<
169+
"' in TopicFilter. \nError: " << e.what());
163170
return false;
164171
}
165172

0 commit comments

Comments
 (0)