Skip to content

Commit 6797a35

Browse files
islandryumarco-ippolito
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 bbd0222 commit 6797a35

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/internal/modules/esm/worker.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
212212
(port ?? syncCommPort).postMessage(wrapMessage('error', exception));
213213
}
214214

215-
AtomicsAdd(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
216-
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
217215
if (shouldRemoveGlobalErrorHandler) {
218216
process.off('uncaughtException', errorHandler);
219217
}
@@ -222,6 +220,10 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
222220
// We keep checking for new messages to not miss any.
223221
clearImmediate(immediate);
224222
immediate = setImmediate(checkForMessages).unref();
223+
// To prevent the main thread from terminating before this function completes after unlocking,
224+
// the following process is executed at the end of the function.
225+
AtomicsAdd(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
226+
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
225227
}
226228
}
227229

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

+16
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,20 @@ describe('Loader hooks parsing modules', { concurrency: true }, () => {
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

+7
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)