Skip to content

Commit 5665e86

Browse files
islandryutargos
authored andcommittedDec 13, 2024
module: prevent main thread exiting before esm worker ends
PR-URL: #56183 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 683cc15 commit 5665e86

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
@@ -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

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

+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)
Please sign in to comment.