Skip to content

Commit 9b1ca62

Browse files
authored
fix(cie_thread_configurator): make spawn_non_ros2_thread node name unique using thread_name (#1243)
* fix(cie_thread_configurator): make spawn_non_ros2_thread node name unique using thread_name Signed-off-by: atsushi421 <atsushi.yano.2@tier4.jp> * fix: address review findings - Add doc comment to sanitize_node_name explaining its behavior - Add unit tests for sanitize_node_name (5 cases) Signed-off-by: atsushi421 <atsushi.yano.2@tier4.jp> * fix(cie_thread_configurator): address review findings - Replace locale-dependent std::isalnum with explicit ASCII range checks in sanitize_node_name() to avoid non-ASCII bytes passing through. - Add missing ament_cmake_gtest test_depend to package.xml. Signed-off-by: atsushi421 <atsushi.yano.2@tier4.jp> --------- Signed-off-by: atsushi421 <atsushi.yano.2@tier4.jp>
1 parent ccd6d60 commit 9b1ca62

4 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/agnocast_cie_thread_configurator/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ install(TARGETS thread_configurator_node DESTINATION lib/${PROJECT_NAME})
4545
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
4646
ament_export_include_directories(include)
4747

48+
if(BUILD_TESTING)
49+
find_package(ament_cmake_gtest REQUIRED)
50+
ament_add_gtest(test_${PROJECT_NAME} test/test_sanitize_node_name.cpp)
51+
target_link_libraries(test_${PROJECT_NAME} thread_configurator)
52+
endif()
53+
4854
ament_package()

src/agnocast_cie_thread_configurator/include/agnocast_cie_thread_configurator/cie_thread_configurator.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ size_t get_default_domain_id();
2626
// Create a node for a different domain
2727
rclcpp::Node::SharedPtr create_node_for_domain(size_t domain_id);
2828

29+
// Replace characters that are not alphanumeric or '_' with '_',
30+
// producing a valid ROS 2 node name token.
31+
inline std::string sanitize_node_name(const std::string & name)
32+
{
33+
std::string result;
34+
result.reserve(name.size());
35+
for (char c : name) {
36+
const bool is_alnum =
37+
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
38+
result += (is_alnum || c == '_') ? c : '_';
39+
}
40+
return result;
41+
}
42+
2943
/// Spawn a thread whose scheduling policy can be managed through
3044
/// cie_thread_configurator.
3145
/// Caution: the `thread_name` must be unique among threads managed by
@@ -45,7 +59,8 @@ std::thread spawn_non_ros2_thread(const char * thread_name, F && f, Args &&... a
4559
rclcpp::NodeOptions options;
4660
options.context(context);
4761
auto node = std::make_shared<rclcpp::Node>(
48-
"cie_thread_client", "/agnocast_cie_thread_configurator", options);
62+
"cie_thread_client_" + sanitize_node_name(thread_name), "/agnocast_cie_thread_configurator",
63+
options);
4964

5065
auto publisher = node->create_publisher<agnocast_cie_config_msgs::msg::NonRosThreadInfo>(
5166
"/agnocast_cie_thread_configurator/non_ros_thread_info", rclcpp::QoS(5000).reliable());

src/agnocast_cie_thread_configurator/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<buildtool_depend>ament_cmake</buildtool_depend>
1818

19+
<test_depend>ament_cmake_gtest</test_depend>
1920
<test_depend>ament_lint_auto</test_depend>
2021
<test_depend>ament_lint_common</test_depend>
2122

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "agnocast_cie_thread_configurator/cie_thread_configurator.hpp"
2+
3+
#include <gtest/gtest.h>
4+
5+
using agnocast_cie_thread_configurator::sanitize_node_name;
6+
7+
TEST(SanitizeNodeName, AlphanumericPassthrough)
8+
{
9+
EXPECT_EQ(sanitize_node_name("abc123"), "abc123");
10+
}
11+
12+
TEST(SanitizeNodeName, UnderscorePreserved)
13+
{
14+
EXPECT_EQ(sanitize_node_name("my_thread"), "my_thread");
15+
}
16+
17+
TEST(SanitizeNodeName, SpecialCharsReplaced)
18+
{
19+
EXPECT_EQ(sanitize_node_name("my-thread.1"), "my_thread_1");
20+
}
21+
22+
TEST(SanitizeNodeName, AllSpecialChars)
23+
{
24+
EXPECT_EQ(sanitize_node_name("---"), "___");
25+
}
26+
27+
TEST(SanitizeNodeName, EmptyString)
28+
{
29+
EXPECT_EQ(sanitize_node_name(""), "");
30+
}

0 commit comments

Comments
 (0)