Exception when destroying an entity that fired an event

I have a super simple repro here: https://playcanvas.com/editor/scene/617417

here is also the whole code:

var EventIssueButton = pc.createScript('eventIssueButton');

// initialize code called once per entity
EventIssueButton.prototype.initialize = function() {
    this.entity.on('run', this.run, this);
    
    window.setTimeout(function() {
        this.entity.fire('run');
    }.bind(this), 1000);
};

EventIssueButton.prototype.run = function() {
    this.entity.destroy();
};

this._callbackActive is null.

any hint?

My best guess is that because it is still running through a loop of possible callbacks on the event, deleting the entity where it is still trying to iterate through it’s list of callbacks effective removes the list while the app is still going through it.

E.g What if another object is subscribed to that event but is after the callback that destroys the caller entity?

I would mark the entity to be destroyed and do it in the next frame.

Thanks. Yes I was supposing was something like this. I’ll try to use a setTimout at zero ms to destroy the entity.

I faced that too.

You can disable listeners right before destroy by .off.

But then you think, why don’t engine do that?
So, long story short, nobody think it should be solved.