diff --git a/NativeScript/runtime/ModuleInternalCallbacks.mm b/NativeScript/runtime/ModuleInternalCallbacks.mm index 51f41ebe..3c69dd28 100644 --- a/NativeScript/runtime/ModuleInternalCallbacks.mm +++ b/NativeScript/runtime/ModuleInternalCallbacks.mm @@ -1076,7 +1076,8 @@ bool HasPendingAsyncModuleGraphWork() { // load's shared_ptr past teardown, so the slot destructor alone cannot cover // these. Called from QuiesceModuleLoadsForIsolate. static void KillAsyncGraphLoadsForIsolate(v8::Isolate* isolate) { - auto* state = ModuleLoaderStateFor(isolate); + // Runtime invalidates the Caches before quiescing, so StateFor would be null. + auto* state = Caches::ExistingStateFor(isolate); if (state == nullptr) { return; } diff --git a/TestRunner/app/tests/HttpEsmLoaderTests.js b/TestRunner/app/tests/HttpEsmLoaderTests.js index 8f398dea..91e6aab4 100644 --- a/TestRunner/app/tests/HttpEsmLoaderTests.js +++ b/TestRunner/app/tests/HttpEsmLoaderTests.js @@ -363,6 +363,64 @@ describe("HTTP ESM Loader", function() { }); }); + describe("worker teardown", function () { + var originalTimeout; + beforeEach(function () { + originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; + jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; + }); + afterEach(function () { + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; + }); + + it("survives HTTP module fetches that land after their worker terminated", + function (done) { + var origin = getHostOrigin(); + if (!origin) { + pending("REPORT_BASEURL not set; skipping host HTTP tests"); + done(); + return; + } + var workerDelayMs = 2000; + var workerCount = 5; + var remaining = workerCount; + + // Delayed past every worker response, so by the time it + // settles all of them have arrived at their torn-down loaders. + function importOnMain() { + import(origin + "/esm/slow/leaf.mjs?k=wt-main&delayMs=" + (workerDelayMs + 1000)) + .then(function (mod) { + expect(mod.value).toBe(42); + done(); + }).catch(function (error) { + expect("rejected: " + formatError(error)).toBe("resolved"); + done(); + }); + } + + function settle(worker) { + worker.terminate(); + if (--remaining === 0) { + importOnMain(); + } + } + + for (var i = 0; i < workerCount; i++) { + (function (worker, k) { + worker.onmessage = function () { + settle(worker); + }; + worker.onerror = function (error) { + fail("worker " + k + " errored: " + formatError(error)); + settle(worker); + }; + worker.postMessage(origin + "/esm/slow/leaf.mjs?k=" + k + + "&delayMs=" + workerDelayMs); + })(new Worker("./httpImportTerminateWorker.js"), "wt" + i); + } + }); + }); + // The import map is process-wide, so every spec here installs its own // and restores the empty map afterwards. // Each present configureLoader section replaces its state wholesale, so diff --git a/TestRunner/app/tests/httpImportTerminateWorker.js b/TestRunner/app/tests/httpImportTerminateWorker.js new file mode 100644 index 00000000..5df1330c --- /dev/null +++ b/TestRunner/app/tests/httpImportTerminateWorker.js @@ -0,0 +1,6 @@ +// Starts an HTTP dynamic import and reports as soon as its fetch is in flight, +// without waiting for it to settle. +onmessage = function (msg) { + import(msg.data).catch(function () {}); + postMessage("fetching"); +};