Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: propagate process.exit from the loader thread to the main thread #47548

Merged
merged 2 commits into from Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/internal/modules/esm/hooks.js
Expand Up @@ -503,6 +503,7 @@ class HooksProxy {
},
});
this.#worker.unref(); // ! Allows the process to eventually exit.
this.#worker.on('exit', process.exit);
}

#waitForWorker() {
Expand All @@ -512,6 +513,7 @@ class HooksProxy {
debug('wait for signal from worker');
AtomicsWait(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 0);
const response = this.#worker.receiveMessageSync();
if (response.message.status === 'exit') return;
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
const { preloadScripts } = this.#unwrapMessage(response);
this.#executePreloadScripts(preloadScripts);
}
Expand Down
13 changes: 13 additions & 0 deletions lib/internal/modules/esm/worker.js
Expand Up @@ -66,6 +66,19 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
let hooks, preloadScripts, initializationError;
let hasInitializationError = false;

{
// If a custom hook is calling `process.exit`, we should wake up the main thread
// so it can detect the exit event.
const { exit } = process;
process.exit = function(code) {
syncCommPort.postMessage(wrapMessage('exit'));
AtomicsAdd(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
return ReflectApply(exit, this, arguments);
};
}


try {
initializeESM();
const initResult = await initializeHooks();
Expand Down
31 changes: 31 additions & 0 deletions test/es-module/test-esm-loader-hooks.mjs
Expand Up @@ -195,4 +195,35 @@ describe('Loader hooks', { concurrency: true }, () => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should be fine to call `process.exit` from a custom hook', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-import-meta-resolve',
'--experimental-loader',
'data:text/javascript,export function resolve(a,b,next){if(a==="exit")process.exit(42);return next(a,b)}',
'--input-type=module',
'--eval',
'import.meta.resolve("exit")',
]);

assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 42);
assert.strictEqual(signal, null);
});

it('should be fine to call `process.exit` from the loader thread top-level', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
'data:text/javascript,process.exit(42)',
fixtures.path('empty.js'),
]);

assert.strictEqual(stderr, '');
assert.strictEqual(stdout, '');
assert.strictEqual(code, 42);
assert.strictEqual(signal, null);
});
});