From 6f08aee8f659e84c555fa26e4c99394319706089 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Tue, 8 Nov 2022 12:17:34 -0500 Subject: [PATCH] Fix zombie scripts (#203) Also made installAugmentations synchronously perform reset, but added a short timeout to the callback script launch for balance. --- src/Netscript/killWorkerScript.ts | 10 ++-------- src/NetscriptFunctions/Singularity.ts | 18 ++++-------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/Netscript/killWorkerScript.ts b/src/Netscript/killWorkerScript.ts index 2c0f8c365..27814340a 100644 --- a/src/Netscript/killWorkerScript.ts +++ b/src/Netscript/killWorkerScript.ts @@ -107,14 +107,8 @@ function removeWorkerScript(workerScript: WorkerScript): void { server.updateRamUsed(0); for (const rs of server.runningScripts) server.updateRamUsed(server.ramUsed + rs.ramUsage * rs.threads); - // Delete script from global pool (workerScripts) - workerScripts.delete(workerScript.pid); - // const res = workerScripts.delete(workerScript.pid); - // if (!res) { - // console.warn(`removeWorkerScript() called with WorkerScript that wasn't in the global map:`); - // console.warn(workerScript); - // } + // Delete script from global pool (workerScripts) after verifying it's the right script (PIDs reset on aug install) + if (workerScripts.get(workerScript.pid) === workerScript) workerScripts.delete(workerScript.pid); AddRecentScript(workerScript); - WorkerScriptStartStopEventEmitter.emit(); } diff --git a/src/NetscriptFunctions/Singularity.ts b/src/NetscriptFunctions/Singularity.ts index 56b80df95..1107bc318 100644 --- a/src/NetscriptFunctions/Singularity.ts +++ b/src/NetscriptFunctions/Singularity.ts @@ -5,7 +5,6 @@ import { Augmentation } from "../Augmentation/Augmentation"; import { StaticAugmentations } from "../Augmentation/StaticAugmentations"; import { augmentationExists, installAugmentations } from "../Augmentation/AugmentationHelpers"; import { AugmentationNames } from "../Augmentation/data/AugmentationNames"; -import { killWorkerScript } from "../Netscript/killWorkerScript"; import { CONSTANTS } from "../Constants"; import { isString } from "../utils/helpers/isString"; import { RunningScript } from "../Script/RunningScript"; @@ -213,12 +212,8 @@ export function NetscriptSingularity(): InternalAPI { const cbScript = _cbScript ? helpers.string(ctx, "cbScript", _cbScript) : ""; helpers.log(ctx, () => "Soft resetting. This will cause this script to be killed"); - setTimeout(() => { - installAugmentations(true); - runAfterReset(cbScript); - }, 0); - - killWorkerScript(ctx.workerScript); + installAugmentations(true); + if (cbScript) setTimeout(() => runAfterReset(cbScript), 500); }, installAugmentations: (ctx) => (_cbScript) => { helpers.checkSingularityAccess(ctx); @@ -230,13 +225,8 @@ export function NetscriptSingularity(): InternalAPI { } Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain * 10); helpers.log(ctx, () => "Installing Augmentations. This will cause this script to be killed"); - setTimeout(() => { - installAugmentations(); - runAfterReset(cbScript); - }, 0); - - killWorkerScript(ctx.workerScript); - return true; + installAugmentations(); + if (cbScript) setTimeout(() => runAfterReset(cbScript), 500); }, goToLocation: (ctx) => (_locationName) => {