Skip to content

fix: Fixed possible memory violation after remove_node #2767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions rclcpp/include/rclcpp/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,28 @@ class Executor
* \param[in] notify True to trigger the interrupt guard condition and wake up the executor.
* This is useful if the last node was removed from the executor while the executor was blocked
* waiting for work in another thread, because otherwise the executor would never be notified.
* \param[in] wait_until_removed If true and the executor is spinning, the method will block until
* all entities from the node have been removed from the executor. Note, if the remove_node call
* was triggered from a callback of an entity of the node itself, and this value is true, this is
* likely to result in a deadlock.
* \throw std::runtime_error if the node is not associated with an executor.
* \throw std::runtime_error if the node is not associated with this executor.
*/
RCLCPP_PUBLIC
virtual void
remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify = true);
remove_node(
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify = true,
bool wait_until_removed = true);

/// Convenience function which takes Node and forwards NodeBaseInterface.
/**
* \see rclcpp::Executor::remove_node
*/
RCLCPP_PUBLIC
virtual void
remove_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true);
remove_node(
std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true,
bool wait_until_removed = true);

/// Add a node to executor, execute the next available unit of work, and remove the node.
/**
Expand Down
20 changes: 17 additions & 3 deletions rclcpp/src/rclcpp/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ Executor::add_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify)
}

void
Executor::remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify)
Executor::remove_node(
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify,
bool wait_until_removed)
{
this->collector_.remove_node(node_ptr);

Expand All @@ -231,12 +233,24 @@ Executor::remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node
std::string(
"Failed to handle entities update on node remove: ") + ex.what());
}

if(wait_until_removed) {
// some other thread is spinning, wait until the executor
// picked up the remove
while(spinning && collector_.has_pending()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
// if we are not spinning, we can directly update the collection
if(!spinning) {
this->collector_.update_collections();
}
}
}

void
Executor::remove_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify)
Executor::remove_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify, bool wait_until_removed)
{
this->remove_node(node_ptr->get_node_base_interface(), notify);
this->remove_node(node_ptr->get_node_base_interface(), notify, wait_until_removed);
}

void
Expand Down
68 changes: 68 additions & 0 deletions rclcpp/test/rclcpp/test_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,71 @@ TEST_F(TestExecutor, is_spinning) {

ASSERT_TRUE(timer_called);
}

TEST_F(TestExecutor, remove_node) {
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>("remove_node_test");
std::vector<rclcpp::TimerBase::SharedPtr> timers;

std::atomic_bool timer_running = false;
auto timer = node->create_timer(std::chrono::milliseconds(1), [&timer_running] () {
timer_running = true;
std::this_thread::sleep_for(std::chrono::milliseconds(400));
timer_running = false;
});
timer->reset();

executor.add_node(node);

while(!timer_running) {
// let the executor pick up the nodes
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
ASSERT_GT(timer.use_count(), 1);

executor.remove_node(node, true, true);

ASSERT_EQ(timer.use_count(), 1);

std::future_status future_status = std::future_status::timeout;
do {
executor.cancel();
future_status = future.wait_for(1s);
} while (future_status == std::future_status::timeout);
EXPECT_EQ(future_status, std::future_status::ready);
future.get();
}

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

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

auto node = std::make_shared<rclcpp::Node>("remove_node_test");
std::vector<rclcpp::TimerBase::SharedPtr> timers;

bool executed = false;
auto timer = node->create_timer(std::chrono::milliseconds(1), [&executed] () {
executed = true;
});
timer->reset();

executor.add_node(node);

while(!executed) {
// let the executor pick up the nodes
executor.spin_some(std::chrono::milliseconds(2));
}

executor.remove_node(node, true, true);

ASSERT_EQ(timer.use_count(), 1);

}