Skip to content

Commit 118773c

Browse files
committed
Release ownership of entities after spinning cancelled
Signed-off-by: Barry Xu <[email protected]>
1 parent c743c17 commit 118773c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

rclcpp/src/rclcpp/executor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,8 @@ Executor::get_next_executable(AnyExecutable & any_executable, std::chrono::nanos
885885
// Wait for subscriptions or timers to work on
886886
wait_for_work(timeout);
887887
if (!spinning.load()) {
888+
// Clear wait result to release ownership of entity
889+
wait_result_.reset();
888890
return false;
889891
}
890892
// Try again

rclcpp/test/rclcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ ament_add_gtest(test_executor test_executor.cpp
641641
TIMEOUT 120)
642642
ament_add_test_label(test_executor mimick)
643643
if(TARGET test_executor)
644-
target_link_libraries(test_executor ${PROJECT_NAME} mimick)
644+
target_link_libraries(test_executor ${PROJECT_NAME} mimick ${test_msgs_TARGETS})
645645
endif()
646646

647647
ament_add_gtest(test_graph_listener test_graph_listener.cpp)

rclcpp/test/rclcpp/test_executor.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "rclcpp/memory_strategy.hpp"
2424
#include "rclcpp/executors/single_threaded_executor.hpp"
2525
#include "rclcpp/strategies/allocator_memory_strategy.hpp"
26+
#include "test_msgs/srv/empty.hpp"
2627

2728
#include "../mocking_utils/patch.hpp"
2829
#include "../utils/rclcpp_gtest_macros.hpp"
@@ -508,3 +509,25 @@ TEST_F(TestExecutor, is_spinning) {
508509

509510
ASSERT_TRUE(timer_called);
510511
}
512+
513+
TEST_F(TestExecutor, release_ownership_entity_after_spinning_cancel) {
514+
using namespace std::chrono_literals;
515+
516+
// Create an Executor
517+
rclcpp::executors::SingleThreadedExecutor executor;
518+
519+
auto future = std::async(std::launch::async, [&executor] {executor.spin();});
520+
521+
auto node = std::make_shared<rclcpp::Node>("test_node");
522+
auto callback = [](
523+
const test_msgs::srv::Empty::Request::SharedPtr, test_msgs::srv::Empty::Response::SharedPtr) {
524+
};
525+
auto server = node->create_service<test_msgs::srv::Empty>("test_service", callback);
526+
executor.add_node(node);
527+
std::this_thread::sleep_for(50ms);
528+
executor.cancel();
529+
std::future_status future_status = future.wait_for(1s);
530+
EXPECT_EQ(future_status, std::future_status::ready);
531+
532+
EXPECT_EQ(server.use_count(), 1);
533+
}

0 commit comments

Comments
 (0)