Skip to content

Commit

Permalink
Release ownership of entities after spinning cancelled
Browse files Browse the repository at this point in the history
Signed-off-by: Barry Xu <barry.xu@sony.com>
  • Loading branch information
Barry-Xu-2018 committed Jun 10, 2024
1 parent 7c09688 commit 5cf00c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions rclcpp/src/rclcpp/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ Executor::get_next_executable(AnyExecutable & any_executable, std::chrono::nanos
// Wait for subscriptions or timers to work on
wait_for_work(timeout);
if (!spinning.load()) {
// Clear wait result to release ownership of entity
wait_result_.reset();
return false;
}
// Try again
Expand Down
2 changes: 1 addition & 1 deletion rclcpp/test/rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ ament_add_gtest(test_executor test_executor.cpp
TIMEOUT 120)
ament_add_test_label(test_executor mimick)
if(TARGET test_executor)
target_link_libraries(test_executor ${PROJECT_NAME} mimick)
target_link_libraries(test_executor ${PROJECT_NAME} mimick ${test_msgs_TARGETS})
endif()

ament_add_gtest(test_graph_listener test_graph_listener.cpp)
Expand Down
23 changes: 23 additions & 0 deletions rclcpp/test/rclcpp/test_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rclcpp/memory_strategy.hpp"
#include "rclcpp/executors/single_threaded_executor.hpp"
#include "rclcpp/strategies/allocator_memory_strategy.hpp"
#include "test_msgs/srv/empty.hpp"

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

ASSERT_TRUE(timer_called);
}

TEST_F(TestExecutor, release_ownership_entity_after_spinning_cancel) {
using namespace std::chrono_literals;

// Create an Executor
rclcpp::executors::SingleThreadedExecutor executor;

auto future = std::async(std::launch::async, [&executor] {executor.spin();});

auto node = std::make_shared<rclcpp::Node>("test_node");
auto callback = [](
const test_msgs::srv::Empty::Request::SharedPtr, test_msgs::srv::Empty::Response::SharedPtr) {
};
auto server = node->create_service<test_msgs::srv::Empty>("test_service", callback);
executor.add_node(node);
std::this_thread::sleep_for(50ms);
executor.cancel();
std::future_status future_status = future.wait_for(1s);
EXPECT_EQ(future_status, std::future_status::ready);

EXPECT_EQ(server.use_count(), 1);
}

0 comments on commit 5cf00c9

Please sign in to comment.