bitburner-official / bitburner-src

Bitburner source code.
Other
838 stars 273 forks source link

API: Scripts using singularity on startup can throw Error: "Router called before initialization - toPage(..." #1707

Open alainbryden opened 1 month ago

alainbryden commented 1 month ago

Just capturing what's been discussed in Discord.

Player scripts invoked on startup (either because their running state is part of the save, or they are set as the startup script) can run before the game is fully initialized, and this can lead to an error if one of them triggers routing.

Normally, this error silently goes unnoticed (because the game's error handling isn't in place yet?), so the end-result is that the script that should have run on startup crashes and appears to have not run.

If the user script has any kind of error-handling though, they would catch an error like this: image

Here's a sample script that can be used to reproduce the error fairly reliably just by running it, saving, then refreshing the game:

export async function main(ns) {
    try {
        ns.singularity.workForFaction("Tian Di Hui", "hacking");
        while (true)
            await ns.sleep(10000);
    }
    catch (err) {
        ns.tprint("ERROR: An unexpected error occurred and now this script must terminate:\n" + (`${err.message}\n${err.stack}` ?? err))
    }
}

You might have to update the faction to one you've joined.


As discussed, some possible solutions are:

d0sboots commented 1 month ago

Additional context: This same error is what causes several of our tests to be flaky. If the game takes long enough to load, it can crash with this error (even the production game, if it takes super long and hits the right weird circumstances - I saw it happen once).

We really ought to fix this at the source (somehow).