From 20d239c596c222dcae667c44118b11d86eb15007 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Thu, 19 Nov 2026 23:37:55 -0300 Subject: [PATCH] fix(runtime): quiesce in-flight http module loads when a worker terminates ~Runtime invalidates the isolate's Caches before it quiesces module loads, so KillAsyncGraphLoadsForIsolate's StateFor lookup returned null and the quiesce did nothing: in-flight async graph loads were neither marked dead nor had their context Globals reset. When a worker was terminated while an HTTP module fetch was in flight, the NSURLSession completion later dropped the last reference to the AsyncGraphLoad on the delegate queue and destroyed its Global after the isolate was disposed (V8_Fatal in GlobalHandles NodeSpace::Release). Look the loader state up with ExistingStateFor, which still returns it after invalidation. --- .../runtime/ModuleInternalCallbacks.mm | 3 +- TestRunner/app/tests/HttpEsmLoaderTests.js | 58 +++++++++++++++++++ .../app/tests/httpImportTerminateWorker.js | 6 ++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 TestRunner/app/tests/httpImportTerminateWorker.js diff --git a/NativeScript/runtime/ModuleInternalCallbacks.mm b/NativeScript/runtime/ModuleInternalCallbacks.mm index 51f41ebe9..3c69dd28b 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 8f398dea0..91e6aab4e 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 000000000..5df1330cc --- /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"); +};