Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System plugins keep executing after their model is removed. #2217

Open
jrutgeer opened this issue Oct 24, 2023 · 5 comments · May be fixed by #2232
Open

System plugins keep executing after their model is removed. #2217

jrutgeer opened this issue Oct 24, 2023 · 5 comments · May be fixed by #2232
Assignees
Labels
bug Something isn't working

Comments

@jrutgeer
Copy link
Contributor

jrutgeer commented Oct 24, 2023

Environment

  • OS Version: Ubuntu 22.04
  • Harmonic, source built

Description

Many system plugins need to be attached to a model (e.g. to access that model's links, collisions, etc).
However, when that model is removed, the plugin keeps running.
Given typically a model plugin needs to access entities of the model (that no longer exist), this leads to crashes.

Expected behavior

When a model is removed, all of its plugins are removed as well, or at the least their pre/update/post methods are no longer called.

Steps to reproduce

  • Create a trivial system plugin, e.g.
void TrivialPlugin::PreUpdate(const gz::sim::UpdateInfo &_info,
                         gz::sim::EntityComponentManager &_ecm)
{
  if (_info.paused) return;

  static int counter = 0;
  counter++;
  if (1000 == counter)
  {
    counter = 0;
    std::cout << "TrivialPlugin::PreUpdate" << std::endl;
  }
}
  • Add this plugin to a model.

  • Start the simulation

  • Select the model in the Entity Tree, right click + 'remove'

  • The model is removed, but the cout message is still being output to the terminal.

@jrutgeer
Copy link
Contributor Author

I implemented a simple workaround like this:

void WasteBin::PostUpdate(const UpdateInfo & _info, const EntityComponentManager & _ecm)
{
  if (_info.paused) return;
  if (!this->dataPtr->configured) return;

  if (!this->dataPtr->model.Valid(_ecm))
  {
    gzwarn << "Model <" << this->dataPtr->modelName << "> no longer valid. "
           << "Disabling WasteBin plugin." << std::endl;
    this->dataPtr->configured = false;
    return;
  }

  [...]

}

While this works well, it has following drawbacks:

  • It is yet some additional code to implement in each model or sensor plugin; prone to be forgotten etc.
  • I assume the call to model.Valid(_ecm) has a non-negligable computational cost, which would be avoided if a proper solution was implemented.

So I still feel that model and sensor plugins should be disabled (or removed) automatically upon removal of the corresponding model or sensor.

@jrutgeer
Copy link
Contributor Author

jrutgeer commented Oct 25, 2023

Another example:

  • Add a model with some joints having a JointPositionController plugin,
  • Remove that model

Result:

Flood of error messages:

[Err] [EntityComponentManager.cc:1083] Trying to create a component of type [8319580315957903596] attached to entity [47], but this entity does not exist. This create component request will be ignored.


I 'fixed' this in my local tree with a similar workaround as above, but meh...

void JointPositionController::PreUpdate(
    const UpdateInfo &_info,
    EntityComponentManager &_ecm)
{
  GZ_PROFILE("JointPositionController::PreUpdate");

  if (kNullEntity == this->dataPtr->model.Entity())
  {
    return;
  }

  if (!this->dataPtr->model.Valid(_ecm))
  {
    gzwarn << "JointPositionController model no longer valid. "
           << "Disabling plugin." << std::endl;
    this->dataPtr->model = Model(kNullEntity);
    return;
  }

  [...]

}

@arjo129
Copy link
Contributor

arjo129 commented Oct 31, 2023

Yes currently the SystemManager does not support removal of plugins. This seems to be something that can be fixed, but will take some amount of effort.

@arjo129 arjo129 self-assigned this Nov 3, 2023
arjo129 added a commit that referenced this issue Nov 7, 2023
This commit tries to address #2217.

In particular if a user despawns an entity, the associated plugin gets
removed. This should prevent issues like #2165. TBH I'm not sure if this
is the right way forward as a system should technically be able to
access any entity in a traditional ECS. I also recognize that there may
be some performance impact. I will need to quantify this.

Signed-off-by: Arjo Chakravarty <[email protected]>
@arjo129 arjo129 linked a pull request Nov 7, 2023 that will close this issue
8 tasks
jrutgeer added a commit to jrutgeer/gz-sim that referenced this issue Nov 21, 2023
arjo129 pushed a commit that referenced this issue Nov 22, 2023
)

* Temporary fix for #2165 until #2217 is resolved.

Signed-off-by: Johan Rutgeerts <[email protected]>

* Added comment to clarify the added code.

Signed-off-by: Johan Rutgeerts <[email protected]>

---------

Signed-off-by: Johan Rutgeerts <[email protected]>
@frede791
Copy link
Contributor

frede791 commented Dec 7, 2023

Latching on to this: I get an entity does not exist error after removing a model that was spawned using the resource spawner. I think this is probably related to this issue. (Using gazebo garden)

@arjo129
Copy link
Contributor

arjo129 commented Dec 11, 2023

Yeah. That is expected I'm currently working on a fix for it here: #2232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: In progress
Development

Successfully merging a pull request may close this issue.

3 participants