Skip to content

Commit 8a5429c

Browse files
islandryuruyadorno
authored andcommitted
module: prevent main thread exiting before esm worker ends
PR-URL: #56183 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 4adf518 commit 8a5429c

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/internal/modules/esm/worker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
215215
(port ?? syncCommPort).postMessage(wrapMessage('error', exception));
216216
}
217217

218-
AtomicsAdd(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
219-
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
220218
if (shouldRemoveGlobalErrorHandler) {
221219
process.off('uncaughtException', errorHandler);
222220
}
@@ -225,6 +223,10 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
225223
// We keep checking for new messages to not miss any.
226224
clearImmediate(immediate);
227225
immediate = setImmediate(checkForMessages).unref();
226+
// To prevent the main thread from terminating before this function completes after unlocking,
227+
// the following process is executed at the end of the function.
228+
AtomicsAdd(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
229+
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
228230
}
229231
}
230232

test/es-module/test-esm-loader-spawn-promisified.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,20 @@ describe('Loader hooks parsing modules', { concurrency: !process.env.TEST_PARALL
285285
assert.strictEqual(code, 0);
286286
assert.strictEqual(signal, null);
287287
});
288+
289+
it('throw maximum call stack error on the loader', async () => {
290+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
291+
'--no-warnings',
292+
'--experimental-loader',
293+
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
294+
'--input-type=module',
295+
'--eval',
296+
'await import("esmHook/maximumCallStack.mjs")',
297+
]);
298+
299+
assert(stderr.includes('Maximum call stack size exceeded'));
300+
assert.strictEqual(stdout, '');
301+
assert.strictEqual(code, 1);
302+
assert.strictEqual(signal, null);
303+
});
288304
});

test/fixtures/es-module-loaders/hooks-custom.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,12 @@ export function load(url, context, next) {
105105
};
106106
}
107107

108+
if (url.endsWith('esmHook/maximumCallStack.mjs')) {
109+
function recurse() {
110+
recurse();
111+
}
112+
recurse();
113+
}
114+
108115
return next(url);
109116
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy