Skip to content

Commit

Permalink
Backport workaround for reinitialized timers from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Pojomovsky committed May 10, 2024
1 parent 2c9859d commit f8279e3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ struct ExecutorEntitiesCollection

/// Clear the entities collection
void clear();

/// Remove entities that have expired weak ownership
/**
* \return The total number of removed entities
*/
size_t remove_expired_entities();
};

/// Build an entities collection from callback groups
Expand Down
24 changes: 24 additions & 0 deletions rclcpp/src/rclcpp/executors/executor_entities_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ void ExecutorEntitiesCollection::clear()
waitables.clear();
}

size_t ExecutorEntitiesCollection::remove_expired_entities()
{
auto remove_entities = [](auto & collection) -> size_t {
size_t removed = 0;
for (auto it = collection.begin(); it != collection.end(); ) {
if (it->second.entity.expired()) {
++removed;
it = collection.erase(it);
} else {
++it;
}
}
return removed;
};

return
remove_entities(subscriptions) +
remove_entities(timers) +
remove_entities(guard_conditions) +
remove_entities(clients) +
remove_entities(services) +
remove_entities(waitables);
}

void
build_entities_collection(
const std::vector<rclcpp::CallbackGroup::WeakPtr> & callback_groups,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ EventsExecutor::refresh_current_collection(
// Acquire lock before modifying the current collection
std::lock_guard<std::recursive_mutex> lock(collection_mutex_);

current_entities_collection_->remove_expired_entities();

current_entities_collection_->timers.update(
new_collection.timers,
[this](rclcpp::TimerBase::SharedPtr timer) {timers_manager_->add_timer(timer);},
Expand Down

0 comments on commit f8279e3

Please sign in to comment.