Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5665e86

Browse filesBrowse files
islandryutargos
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 683cc15 commit 5665e86
Copy full SHA for 5665e86

File tree

Expand file treeCollapse file tree

3 files changed

+27
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+27
-2
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/worker.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/worker.js
+4-2Lines 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

Collapse file

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

Copy file name to clipboardExpand all lines: test/es-module/test-esm-loader-spawn-promisified.mjs
+16Lines 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
});
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/es-module-loaders/hooks-custom.mjs
+7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.