SETTINGS: Add infinite loop detection setting (#402)

This commit is contained in:
Snarling
2023-02-28 06:54:01 -05:00
committed by GitHub
parent ae92ea0f94
commit ee3c412933
3 changed files with 34 additions and 0 deletions
+21
View File
@@ -33,6 +33,7 @@ import { BaseServer } from "../Server/BaseServer";
import { dialogBoxCreate } from "../ui/React/DialogBox";
import { checkEnum } from "../utils/helpers/enum";
import { RamCostConstants } from "./RamCostGenerator";
import { Settings } from "../Settings/Settings";
export const helpers = {
string,
@@ -303,6 +304,15 @@ function checkSingularityAccess(ctx: NetscriptContext): void {
}
}
/** The last time the page was able to update this tracking variable, which is used for timeout detection */
let lastTime = Date.now();
setInterval(() => (lastTime = Date.now()), 1000);
// This event should prevent false positive timeout errors when user alt-tabs / otherwise hides the window
document.addEventListener(
"visibilitychange",
() => (lastTime = document.visibilityState === "hidden" ? Infinity : Date.now()),
);
/** Create an error if a script is dead or if concurrent ns function calls are made */
function checkEnvFlags(ctx: NetscriptContext): void {
const ws = ctx.workerScript;
@@ -323,6 +333,17 @@ function checkEnvFlags(ctx: NetscriptContext): void {
"CONCURRENCY",
);
}
// 8s allows scripts at least 7s of synchronous execution time before error, since updates are every 1s.
if (Settings.infiniteLoopDetection && Date.now() - 8000 > lastTime) {
// Prevent error getting piggybacked to another script due to a stale timer.
lastTime = Date.now();
throw makeRuntimeErrorMsg(
ctx,
"Possible infinite loop detected while running function.\nThis error is enabled by Options -> System -> Infinite loop detection.",
"EXECUTION TIMEOUT",
);
}
}
/** Set a timeout for performing a task, mark the script as busy in the meantime. */