@@ -275,10 +275,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
275275 switch (event.type ) {
276276 case ExecutorEventType::CLIENT_EVENT:
277277 {
278- auto client = this ->retrieve_entity (
279- static_cast <const rcl_client_t *>(event.entity_key ),
280- current_entities_collection_->clients );
281-
278+ rclcpp::ClientBase::SharedPtr client;
279+ {
280+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
281+ client = this ->retrieve_entity (
282+ static_cast <const rcl_client_t *>(event.entity_key ),
283+ current_entities_collection_->clients );
284+ }
282285 if (client) {
283286 for (size_t i = 0 ; i < event.num_events ; i++) {
284287 execute_client (client);
@@ -289,9 +292,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
289292 }
290293 case ExecutorEventType::SUBSCRIPTION_EVENT:
291294 {
292- auto subscription = this ->retrieve_entity (
293- static_cast <const rcl_subscription_t *>(event.entity_key ),
294- current_entities_collection_->subscriptions );
295+ rclcpp::SubscriptionBase::SharedPtr subscription;
296+ {
297+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
298+ subscription = this ->retrieve_entity (
299+ static_cast <const rcl_subscription_t *>(event.entity_key ),
300+ current_entities_collection_->subscriptions );
301+ }
295302 if (subscription) {
296303 for (size_t i = 0 ; i < event.num_events ; i++) {
297304 execute_subscription (subscription);
@@ -301,10 +308,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
301308 }
302309 case ExecutorEventType::SERVICE_EVENT:
303310 {
304- auto service = this ->retrieve_entity (
305- static_cast <const rcl_service_t *>(event.entity_key ),
306- current_entities_collection_->services );
307-
311+ rclcpp::ServiceBase::SharedPtr service;
312+ {
313+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
314+ service = this ->retrieve_entity (
315+ static_cast <const rcl_service_t *>(event.entity_key ),
316+ current_entities_collection_->services );
317+ }
308318 if (service) {
309319 for (size_t i = 0 ; i < event.num_events ; i++) {
310320 execute_service (service);
@@ -321,9 +331,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
321331 }
322332 case ExecutorEventType::WAITABLE_EVENT:
323333 {
324- auto waitable = this ->retrieve_entity (
325- static_cast <const rclcpp::Waitable *>(event.entity_key ),
326- current_entities_collection_->waitables );
334+ rclcpp::Waitable::SharedPtr waitable;
335+ {
336+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
337+ waitable = this ->retrieve_entity (
338+ static_cast <const rclcpp::Waitable *>(event.entity_key ),
339+ current_entities_collection_->waitables );
340+ }
327341 if (waitable) {
328342 for (size_t i = 0 ; i < event.num_events ; i++) {
329343 auto data = waitable->take_data_by_entity_id (event.waitable_data );
@@ -388,6 +402,7 @@ EventsExecutor::get_automatically_added_callback_groups_from_nodes()
388402void
389403EventsExecutor::refresh_current_collection_from_callback_groups ()
390404{
405+ // Build the new collection
391406 this ->entities_collector_ ->update_collections ();
392407 auto callback_groups = this ->entities_collector_ ->get_all_callback_groups ();
393408 rclcpp::executors::ExecutorEntitiesCollection new_collection;
@@ -402,6 +417,9 @@ EventsExecutor::refresh_current_collection_from_callback_groups()
402417 // To do it, we need to add the notify waitable as an entry in both the new and
403418 // current collections such that it's neither added or removed.
404419 this ->add_notify_waitable_to_collection (new_collection.waitables );
420+
421+ // Acquire lock before modifying the current collection
422+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
405423 this ->add_notify_waitable_to_collection (current_entities_collection_->waitables );
406424
407425 this ->refresh_current_collection (new_collection);
411429EventsExecutor::refresh_current_collection (
412430 const rclcpp::executors::ExecutorEntitiesCollection & new_collection)
413431{
432+ // Acquire lock before modifying the current collection
433+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
434+
414435 current_entities_collection_->timers .update (
415436 new_collection.timers ,
416437 [this ](rclcpp::TimerBase::SharedPtr timer) {timers_manager_->add_timer (timer);},
0 commit comments