@@ -371,7 +371,14 @@ Executor::spin_some_impl(std::chrono::nanoseconds max_duration, bool exhaustive)
371371 // both spin_some and spin_all wait for work at the beginning
372372 wait_result_.reset ();
373373 wait_for_work (std::chrono::milliseconds (0 ));
374- bool just_waited = true ;
374+ bool entity_states_fully_polled = true ;
375+
376+ if (entities_need_rebuild_) {
377+ // if the last wait triggered a collection rebuild, we need to call
378+ // wait_for_work once more, in order to do a collection rebuild and collect
379+ // events from the just added entities
380+ entity_states_fully_polled = false ;
381+ }
375382
376383 // The logic of this while loop is as follows:
377384 //
@@ -393,12 +400,14 @@ Executor::spin_some_impl(std::chrono::nanoseconds max_duration, bool exhaustive)
393400 AnyExecutable any_exec;
394401 if (get_next_ready_executable (any_exec)) {
395402 execute_any_executable (any_exec);
396- just_waited = false ;
403+ // during the execution some entity might got ready therefore we need
404+ // to repoll the states of all entities
405+ entity_states_fully_polled = false ;
397406 } else {
398407 // if nothing is ready, reset the result to clear it
399408 wait_result_.reset ();
400409
401- if (just_waited ) {
410+ if (entity_states_fully_polled ) {
402411 // there was no work after just waiting, always exit in this case
403412 // before the exhaustive condition can be checked
404413 break ;
@@ -408,7 +417,13 @@ Executor::spin_some_impl(std::chrono::nanoseconds max_duration, bool exhaustive)
408417 // if exhaustive, wait for work again
409418 // this only happens for spin_all; spin_some only waits at the start
410419 wait_for_work (std::chrono::milliseconds (0 ));
411- just_waited = true ;
420+ entity_states_fully_polled = true ;
421+ if (entities_need_rebuild_) {
422+ // if the last wait triggered a collection rebuild, we need to call
423+ // wait_for_work once more, in order to do a collection rebuild and
424+ // collect events from the just added entities
425+ entity_states_fully_polled = false ;
426+ }
412427 } else {
413428 break ;
414429 }
0 commit comments