>=3.15.0 BUG: Scene restarts on exception

I have uncovered what seems like a major bug in versions 3.15.0 and 3.15.1, where under certain conditions Scenes will continually restart in an infinite loop when an exception is raised. The detailed setup to reproduce is below, but the gist is as follows:

  1. Create two Scene classes (e.g. BootScene and LoadScene) which both inherit from Phaser.Scene

  2. Start the game on the BootScene and then start the LoadScene from the BootScene

  3. Raise an exception somewhere in LoadScene. Either in preload or create, it doesn’t seem to matter. I did this for example by calling a scene function that doesn’t exist, this.foobar()

In Phaser 3.15.0 and above, this will raise the following stack trace, repeated over and over again in an infinite loop. It seems that the scene is continually trying to restart itself on the exception, and thus continually raises the error:

Uncaught TypeError: this.foobar is not a function
    at DemoLoadScene.preload (load.js:10)
    at SceneManager.bootScene (SceneManager.js:474)
    at SceneManager.start (SceneManager.js:1142)
    at SceneManager.processQueue (SceneManager.js:288)
    at SceneManager.update (SceneManager.js:547)
    at Game.step (Game.js:538)
    at TimeStep.step (TimeStep.js:560)
    at step (RequestAnimationFrame.js:104)

In Phaser 3.14.0 and below, this exact same code raises the following error only a single time and then execution stops (i.e. much more sane and expected behavior):

Uncaught TypeError: this.foobar is not a function
    at DemoLoadScene.preload (load.js:10)
    at SceneManager.bootScene (SceneManager.js:474)
    at SceneManager.start (SceneManager.js:1142)
    at SceneManager.processQueue (SceneManager.js:288)
    at SceneManager.update (SceneManager.js:547)
    at Game.step (Game.js:553)
    at TimeStep.step (TimeStep.js:560)
    at step (RequestAnimationFrame.js:102)

It should also be noted that the bad behavior in 3.15.0 does not occur when you raise the error in the BootScene so it seems to only occur in the context of one Scene starting from inside another. This seems like a pretty major and fundamental issue so I’m surprised that no one has raised it before. At the moment my only option is to revert back to 3.14.0 until this is solved.

To reproduce this error, set up a small Phaser app with index.js scenes/boot.js and scenes/load.js as follows:

// index.js
import Phaser from "phaser"
import DemoBootScene from "./scenes/boot.js"
import DemoLoadScene from "./scenes/load.js"

const CONFIG = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
}

let demoApp = new Phaser.Game(CONFIG)

demoApp.scene.add("boot", DemoBootScene)
demoApp.scene.add("load", DemoLoadScene)
demoApp.scene.start("boot")
// scenes/boot.js
import Phaser from "phaser"

class DemoBootScene extends Phaser.Scene {

  constructor() {
    super(...arguments)
  }

  preload() {}

  create() {
    this.scene.start("load")
    this.scene.stop()
  }

  update() {}
}

export default DemoBootScene
// scenes/load.js
import Phaser from "phaser"

class DemoLoadScene extends Phaser.Scene {

  constructor() {
    super(...arguments)
  }

  preload() {
    this.foobar() // deliberately raise an exception
  }

  create() {}

  update() {}
}

export default DemoLoadScene

I can reproduce it too, here’s an example: https://jsfiddle.net/billnreed/zmfovn46/1/

I’m not surprised no one has brought this up though, because if an exception is being thrown, it means that there is something going wrong, and so the game is now in an invalid state. I’m surprised you are blocked by this bug since that means you had an exception being thrown in your scene creation. Sounds like that should be the main issue you need to address.

I’m not blocked entirely, but I don’t want to work in a version of the framework that throws an infinite loop of exceptions on every error. It makes my debug cycles a lot more difficult to work through. Not sure if this only happens on create and preload, I haven’t tested it out in the update loop.

In any case thanks for your fiddle, that’s a much more concise description of the issue! :slight_smile:

No problemo. Since its clearly an issue, will you open an issue on GitHub?

1 Like

In my opinion, you seem to be confusing “Scenes” with “States”. The new Phaser III Scenes are similar to Adobe Flash movie clips in that you can have more than one running at the same time in the “main time line”.

@snowbillr sure I’ll write the bug ticket now

Alrighty, here we go: https://github.com/photonstorm/phaser/issues/4264

I don’t have nearly enough knowledge of Phaser internals at this point to attempt a fix myself, but I’ll try my hand at it if this sits open for a week or more with no activity. Perhaps Richard will chime in and shed some light soon. Thanks for your help!

1 Like

The problem is the Scene Manager can’t advance its operations queue because the exception is blocking it.