Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alainbryden opened this issue Oct 20, 2024 · 1 comment · May be fixed by #1726
Labels
bug Something isn't working

Comments

@alainbryden
Copy link
Contributor

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:

  • Don't start any user scripts until after the game is done initializing (speed runners might be mad)
  • Just for the handful of singularity scripts which route, wrap the attempt to route in a try/catch. You can then either:
    • ignore the attempt to route and move on (might have weird side-effects e.g. relating to focus penalties)
    • Store the route destination in a variable, and once the UI is initialized, fulfill the route request ASAP. If multiple route-attempts occur, just save the most recent one? (I can only imagine this messing with scripts that try to interact with the UI, like auto-infiltration)
@d0sboots
Copy link
Collaborator

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).

@gmcew gmcew added the bug Something isn't working label Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants