# Stopping render loop and then immediately running it again causes double frames

**URL:** <https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599>\
**Category:** Bugs\
**Created:** [March 7, 2024, 3:32pm UTC](https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599 "2024-03-07T15:32:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alvov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/alvov/32/2061_2.png) [@alvov](https://forum.babylonjs.com/u/alvov)\
**Post date:** [March 7, 2024, 3:32pm UTC](https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599/1 "2024-03-07T15:32:14Z")

</div>

Hello,  
I suspect it’s an edge case scenario, but I’ll still give it a try.

In our code there happened a situation when `stopRenderLoop` and `runRenderLoop` were called within the same JS event loop, i.e. almost immediately one after another:

```javascript
engine.stopRenderLoop(renderSceneFunction);
// some synchronous code
engine.runRenderLoop(renderSceneFunction);

```

This caused `renderSceneFunction` to be called twice per frame, i.e. the “first” render loop wasn’t actually stopped.

**Here’s a [PG](https://playground.babylonjs.com/#IJL3AR)**,

where `render` function is called N times in 1 second of rendering (`renderCount` variable),  
but if you comment out the `stopRenderLoop/runRenderLoop` calls it becomes ~ N/2.

I think the change was introduced here [Cancel any pending animation frames on stopRenderLoop by carolhmj · Pull Request #14102 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/14102) (v6.14.2).  
The reason is that `_renderLoop` and `runRenderLoop` methods both schedule another frame in this particular case:

```auto
_renderLoop() {
    // ...
    someRenderFunction();
        // inside `renderFunction` there's a call to `stopRenderLoop`:
        engine.stopRenderLoop(someRenderFunction);
            // inside stopRenderLoop this flag is reset
            this._renderingQueueLaunched = false;
            // and _activeRenderLoops array is emptied
            this._activeRenderLoops.splice(/*...*/);
        // some synchronous code here, then still inside `renderFunction` render loop is resumed
        engine.runRenderLoop(someRenderFunction);
            // inside this function _activeRenderLoops array is filled
            this._activeRenderLoops.push(renderFunction);
            // and new frame is scheduled
            this._frameHandler = this._queueNewFrame(/*...*/);
    // END of `someRenderFunction`
    // but this _renderLoop isn't over yet, it now checks for _activeRenderLoops which is not empty anymore
    if (this._activeRenderLoops.length > 0)
        // and then also schedules another frame
        this._frameHandler = this._queueNewFrame(/*...*/);

```

Previously `this._renderingQueueLaunched` wasn’t set to `false` by `stopRenderLoop` so new frame was only scheduled once.

I’d appreciate any input on that.

Thank you 🙏

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 13, 2024, 8:23pm UTC](https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599/2 "2024-03-13T20:23:32Z")

</div>

I am working on it ASAP, sorry you did not have an answer before.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 13, 2024, 8:32pm UTC](https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599/3 "2024-03-13T20:32:15Z")

</div>

Assigned @sebavan on March 13, 2024, 8:32pm UTC



---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 14, 2024, 1:09pm UTC](https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599/4 "2024-03-14T13:09:18Z")

</div>

Will be fixed by [fix cancel render loop by sebavan · Pull Request #14868 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/14868) but need a bit more checks

---

<div class="post-metadata">

**Author:** ![alvov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/alvov/32/2061_2.png) [@alvov](https://forum.babylonjs.com/u/alvov)\
**Post date:** [March 20, 2024, 8:52am UTC](https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599/5 "2024-03-20T08:52:31Z")

</div>

Thank you very much 🙏

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 20, 2024, 11:41pm UTC](https://forum.babylonjs.com/t/stopping-render-loop-and-then-immediately-running-it-again-causes-double-frames/48599/6 "2024-03-20T23:41:57Z")

</div>

Unassigned @sebavan on March 20, 2024, 11:41pm UTC


