@@ -273,10 +273,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
273
273
switch (event.type ) {
274
274
case ExecutorEventType::CLIENT_EVENT:
275
275
{
276
- auto client = this ->retrieve_entity (
277
- static_cast <const rcl_client_t *>(event.entity_key ),
278
- current_entities_collection_->clients );
279
-
276
+ rclcpp::ClientBase::SharedPtr client;
277
+ {
278
+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
279
+ client = this ->retrieve_entity (
280
+ static_cast <const rcl_client_t *>(event.entity_key ),
281
+ current_entities_collection_->clients );
282
+ }
280
283
if (client) {
281
284
for (size_t i = 0 ; i < event.num_events ; i++) {
282
285
execute_client (client);
@@ -287,9 +290,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
287
290
}
288
291
case ExecutorEventType::SUBSCRIPTION_EVENT:
289
292
{
290
- auto subscription = this ->retrieve_entity (
291
- static_cast <const rcl_subscription_t *>(event.entity_key ),
292
- current_entities_collection_->subscriptions );
293
+ rclcpp::SubscriptionBase::SharedPtr subscription;
294
+ {
295
+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
296
+ subscription = this ->retrieve_entity (
297
+ static_cast <const rcl_subscription_t *>(event.entity_key ),
298
+ current_entities_collection_->subscriptions );
299
+ }
293
300
if (subscription) {
294
301
for (size_t i = 0 ; i < event.num_events ; i++) {
295
302
execute_subscription (subscription);
@@ -299,10 +306,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
299
306
}
300
307
case ExecutorEventType::SERVICE_EVENT:
301
308
{
302
- auto service = this ->retrieve_entity (
303
- static_cast <const rcl_service_t *>(event.entity_key ),
304
- current_entities_collection_->services );
305
-
309
+ rclcpp::ServiceBase::SharedPtr service;
310
+ {
311
+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
312
+ service = this ->retrieve_entity (
313
+ static_cast <const rcl_service_t *>(event.entity_key ),
314
+ current_entities_collection_->services );
315
+ }
306
316
if (service) {
307
317
for (size_t i = 0 ; i < event.num_events ; i++) {
308
318
execute_service (service);
@@ -319,9 +329,13 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
319
329
}
320
330
case ExecutorEventType::WAITABLE_EVENT:
321
331
{
322
- auto waitable = this ->retrieve_entity (
323
- static_cast <const rclcpp::Waitable *>(event.entity_key ),
324
- current_entities_collection_->waitables );
332
+ rclcpp::Waitable::SharedPtr waitable;
333
+ {
334
+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
335
+ waitable = this ->retrieve_entity (
336
+ static_cast <const rclcpp::Waitable *>(event.entity_key ),
337
+ current_entities_collection_->waitables );
338
+ }
325
339
if (waitable) {
326
340
for (size_t i = 0 ; i < event.num_events ; i++) {
327
341
auto data = waitable->take_data_by_entity_id (event.waitable_data );
@@ -386,6 +400,7 @@ EventsExecutor::get_automatically_added_callback_groups_from_nodes()
386
400
void
387
401
EventsExecutor::refresh_current_collection_from_callback_groups ()
388
402
{
403
+ // Build the new collection
389
404
this ->entities_collector_ ->update_collections ();
390
405
auto callback_groups = this ->entities_collector_ ->get_all_callback_groups ();
391
406
rclcpp::executors::ExecutorEntitiesCollection new_collection;
@@ -400,6 +415,9 @@ EventsExecutor::refresh_current_collection_from_callback_groups()
400
415
// To do it, we need to add the notify waitable as an entry in both the new and
401
416
// current collections such that it's neither added or removed.
402
417
this ->add_notify_waitable_to_collection (new_collection.waitables );
418
+
419
+ // Acquire lock before modifying the current collection
420
+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
403
421
this ->add_notify_waitable_to_collection (current_entities_collection_->waitables );
404
422
405
423
this ->refresh_current_collection (new_collection);
409
427
EventsExecutor::refresh_current_collection (
410
428
const rclcpp::executors::ExecutorEntitiesCollection & new_collection)
411
429
{
430
+ // Acquire lock before modifying the current collection
431
+ std::lock_guard<std::recursive_mutex> lock (collection_mutex_);
432
+
412
433
current_entities_collection_->timers .update (
413
434
new_collection.timers ,
414
435
[this ](rclcpp::TimerBase::SharedPtr timer) {timers_manager_->add_timer (timer);},
0 commit comments