Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <algorithm>
#include <chrono>
#include <exception>
#include <functional>
Expand Down Expand Up @@ -253,7 +254,8 @@ void EventsCBGExecutor::trigger_callback_group_sync()
if (!spinning) {
sync_callback_groups();
} else {
scheduler->unblock_one_worker_thread();
// Make pending work visible to workers about to block.
scheduler->trigger_sync();
}
}

Expand Down Expand Up @@ -819,24 +821,26 @@ EventsCBGExecutor::remove_node(
}

{
std::lock_guard lock{added_nodes_mutex_};
added_nodes.erase(
std::remove_if(
added_nodes.begin(), added_nodes.end(), [&node_ptr](const auto & weak_ptr) {
const auto shr_ptr = weak_ptr.lock();
return shr_ptr && shr_ptr == node_ptr;
}), added_nodes.end());
}

node_ptr->for_each_callback_group(
[this](const rclcpp::CallbackGroup::SharedPtr & cbg)
std::lock_guard groups_lock{callback_groups_mutex};
{
unregister_event_callbacks(cbg);
std::lock_guard nodes_lock{added_nodes_mutex_};
std::erase_if(added_nodes, [&node_ptr](const auto & weak_ptr) {
return weak_ptr.lock() == node_ptr;
});
}
);

node_ptr->get_shared_notify_guard_condition()->set_on_trigger_callback(
// A remove and re-add can be coalesced into a single sync. Discard the old
// registrations now so that sync registers the node's entities again.
std::erase_if(
callback_groups, [&node_ptr](const CallbackGroupData & data) {
const auto group = data.callback_group.lock();
return group && data.origin == CallbackGroupData::Origin::Node &&
node_ptr->callback_group_in_node(group);
});

node_ptr->get_shared_notify_guard_condition()->set_on_trigger_callback(
std::function<void(size_t)>());
}

trigger_callback_group_sync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,24 @@ TEST_F(TestEventsCBGExecutorTimerRemoval, remove_and_readd_node_during_spin)
}
EXPECT_GE(calls, calls_after_return + 10);
}

TEST_F(TestEventsCBGExecutorTimerRemoval, readd_node_before_callback_groups_sync)
{
timer->cancel();
int readded_timer_calls = 0;
auto readded_timer = node->create_wall_timer(
1ms, [this, &readded_timer_calls]() {
if (++readded_timer_calls == 1) {
executor->remove_node(node);
executor->add_node(node);
}
});

// spin_some handles both operations in the same callback before it can sync again.
const auto deadline = std::chrono::steady_clock::now() + 2s;
while (readded_timer_calls < 2 && std::chrono::steady_clock::now() < deadline) {
executor->spin_some(5ms);
std::this_thread::sleep_for(1ms);
}
EXPECT_GE(readded_timer_calls, 2);
}