Skip to content

Possible bug: executor_entities_collection.hpp::update_entities #2632

Open

Description

@jmachowinski

Bug report

for (auto it = update_to.begin(); it != update_to.end(); ) {
if (update_from.count(it->first) == 0) {
auto entity = it->second.entity.lock();
if (entity) {
on_removed(entity);
}
it = update_to.erase(it);
} else {

I am confused about the

if (entity) {

in line 78. Would this not lead to the on_removed callback not being called, in race cases, even though the element was removed ?
@wjwwood @mjcarroll Is this intended, or a bug ?

Obviously, it's kind of useless, to call the callback with a nullptr, but the behavior is still strange to me. I ran into this issue, as internal structures of my code were not cleaned up, due to the callback not being called.

Activity

  1. alsora commented on Sep 18, 2024

    @alsora
    Collaborator

    I'm not sure I understand the problem here.

    Currently all executors require that the entity passed to on_removed is not a nullptr, because they need to call a method on it.

  2. jmachowinski commented on Sep 18, 2024

    @jmachowinski
    CollaboratorAuthor

    The problem is, that the documentation of the method / class does not match the behavior.

    It states, one will get a on_removed callback, whenever 'update_to' does not contain the entry.
    This is not the case, if the weak_ptr became invalid in between.

    I noticed this, as I tried to use this class and depended on the behavior, that on_removed is called, to clean up
    other internals of my executor.

    This class is used extensively in the executor. Therefore I was wondering, if there is a place
    somewhere in the executor, were it also depends on 'on_removed' being called, causing 'rare' bugs ?

  3. alsora commented on Sep 18, 2024

    @alsora
    Collaborator

    Ok, thanks for the explanation.
    This class was meant to be used only by the executor, where (at least currently) the problem that you mention doesn't apply.
    Note that the same situation happens with the on_added function (it's called only on valid entities despite what the docs say).

    So essentially we have two options:

    1. fix the documentation to state that the functions are called only on valid entities.
    2. move the check about whether an entity is valid outside the entities collector and into the functions provided by the executor.

    I don't have a strong opinion, although i probably prefer the first.
    The first option is more suitable if we want to reduce code complexity in the executor, while the second option is more suitable if we want to make this class more generic and reusable.

    In any case, we should do something.
    If someone feels strongly about one option or the other, I'm ok with it.

  4. Aaravanand00 commented on Nov 7, 2026

    @Aaravanand00
    Contributor

    @jmachowinski @mjcarroll I raised a pr implements Option 1 from the discussion in this issue by documenting that on_added and on_removed callbacks are only fired when the entity's weak pointer is valid (non-expired). Verified with both ament_cpplint and ament_uncrustify. Ptal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions