# Adding new observer inside the same observable callback causes crash

**URL:** <https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316>\
**Category:** Bugs\
**Created:** [March 20, 2025, 7:28pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316 "2025-03-20T19:28:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khanon](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/khanon/32/14980_2.png) [@Khanon](https://forum.babylonjs.com/u/Khanon)\
**Post date:** [March 20, 2025, 7:28pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/1 "2025-03-20T19:28:07Z")

</div>

Hello,

I’m not sure if this is a bug, but I’ve been doing some tests today, and if I do the next:

```auto
const playAnimation = () => {
    // Clear observable and stop previous animation
    this._animation.animationGroup.onAnimationGroupEndObservable.clear()
    this._animation.animationGroup.stop()

    // Start mesh animation
    this._animation.animationGroup.start()

    // Trigger observable on animation end
    this._animation.animationGroup.onAnimationGroupEndObservable.add(() => playAnimation())
}

```

The third iteration triggers the observable lot of times, and the fourth iteration triggers it non stop.  
I’ve tried different things and I can’t avoid this situation. `notifyIfTriggered` is false.

Instead, using a `setTimeout`, the code above works fine, iteration by iteration.

```auto
const playAnimation = () => {
    this._animation.animationGroup.stop()

    this._animation.animationGroup.start()

    setTimeout(() => {
        playAnimation()
    }, 5000) // Some arbitrary time similar to animation end
}

```

Some idea?  
If you think it worth to be reviewed I’ll create a PG.

PS: Waiting a frame before calling the new observer prevents the situation:

```auto
const playAnimation = () => {
    // Clear observable and stop previous animation
    this._animation.animationGroup.onAnimationGroupEndObservable.clear()
    this._animation.animationGroup.stop()

    // Start mesh animation
    this._animation.animationGroup.start()

    // Trigger observable on animation end, calls callback 1 frame later
    this._animation.animationGroup.onAnimationGroupEndObservable.add(() => {
        setTimeout(playAnimation, 1)
    })
}

```

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [March 20, 2025, 9:18pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/2 "2025-03-20T21:18:26Z")

</div>

I could use a PG to see how to explain it 😃

---

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [March 22, 2025, 11:11am UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/4 "2025-03-22T11:11:52Z")

</div>

Here you go: [https://playground.babylonjs.com/#Z6SWJU#1143](https://playground.babylonjs.com/#Z6SWJU#1143)

Please uncomment playAnimation. Warning: might crash the scene.

 ![Screenshot 2025-03-22 121029](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/1/510c42b8c7f0d77710421ef3ff2b83f4e0b7512e.jpeg)

---

<div class="post-metadata">

**Author:** ![Khanon](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/khanon/32/14980_2.png) [@Khanon](https://forum.babylonjs.com/u/Khanon)\
**Post date:** [March 22, 2025, 12:21pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/5 "2025-03-22T12:21:45Z")

</div>

Thanks, I didn’t have time to build the PG 😅

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [March 24, 2025, 4:28pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/6 "2025-03-24T16:28:15Z")

</div>

let me have a look!

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [March 24, 2025, 5:09pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/7 "2025-03-24T17:09:19Z")

</div>

ok so not a bug per se!

Here is the explanation:

- When the animation ends, it calls notifyObservers on the onAnimationGroupEndObservable
- The observable is doing a loop on its observers
- In this case one observer is clearing the list of observers WITHIN the loop and then add a new observer while still being in the obsersable loop
- The observable loop then check the next observer (which is the new playanimation that we added)
- Infinite loop

This is a classic case of updating a list while running a loop on that list

So the fix to skip to the next event is the good one  
We do not overly protect the observable loops because they are widely used within the framework and impact on perf would be terrible

---

<div class="post-metadata">

**Author:** ![Khanon](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/khanon/32/14980_2.png) [@Khanon](https://forum.babylonjs.com/u/Khanon)\
**Post date:** [March 24, 2025, 5:23pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/8 "2025-03-24T17:23:49Z")

</div>

Got it, so I keep the setTimeout(() =\> next, 1), so users wont have to deal with this scenario 🙂

This is done when the animation ends, so there’s not performance impact.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [March 24, 2025, 5:25pm UTC](https://forum.babylonjs.com/t/adding-new-observer-inside-the-same-observable-callback-causes-crash/57316/9 "2025-03-24T17:25:29Z")

</div>

Exactly! this is the right approach!
