MISC: Make spawn able to have 0 delay (#1333)

This eliminates a hole where spawn was unrelaible, because other scripts
could jump in and steal the RAM. It's not an API break, because 0 used
to be an invalid value.
This commit is contained in:
David Walker
2024-06-28 18:41:41 -07:00
committed by GitHub
parent 06d742a7f3
commit 1c20a24079
6 changed files with 39 additions and 13 deletions
+20 -7
View File
@@ -737,23 +737,36 @@ export const ns: InternalAPI<NSFull> = {
const path = helpers.scriptPath(ctx, "scriptname", _scriptname);
const runOpts = helpers.spawnOptions(ctx, _thread_or_opt);
const args = helpers.scriptArgs(ctx, _args);
setTimeout(() => {
const spawnCb = () => {
if (Router.page() === Page.BitVerse) {
helpers.log(ctx, () => `Script execution is canceled because you are in Bitverse.`);
return;
}
const scriptServer = GetServer(ctx.workerScript.hostname);
if (scriptServer === null) {
if (scriptServer == null) {
throw helpers.errorMessage(ctx, `Cannot find server ${ctx.workerScript.hostname}`);
}
runScriptFromScript("spawn", scriptServer, path, args, ctx.workerScript, runOpts);
}, runOpts.spawnDelay);
return runScriptFromScript("spawn", scriptServer, path, args, ctx.workerScript, runOpts);
};
helpers.log(ctx, () => `Will execute '${path}' in ${runOpts.spawnDelay} milliseconds`);
if (runOpts.spawnDelay !== 0) {
setTimeout(spawnCb, runOpts.spawnDelay);
helpers.log(ctx, () => `Will execute '${path}' in ${runOpts.spawnDelay} milliseconds`);
}
if (killWorkerScript(ctx.workerScript)) {
helpers.log(ctx, () => "Exiting...");
helpers.log(ctx, () => "About to exit...");
const killed = killWorkerScript(ctx.workerScript);
if (runOpts.spawnDelay === 0) {
helpers.log(ctx, () => `Executing '${path}' immediately`);
spawnCb();
}
if (killed) {
// This prevents error messages about statements after the spawn()
// trying to be executed when the script is dead.
throw new ScriptDeath(ctx.workerScript);
}
},
kill: