Skip to content

[v14.x backport] esm: make process.exit() default to exit code 0 #41508

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

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/internal/modules/esm/handle_process_exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

// Handle a Promise from running code that potentially does Top-Level Await.
// In that case, it makes sense to set the exit code to a specific non-zero
// value if the main code never finishes running.
function handleProcessExit() {
process.exitCode ??= 13;
}

module.exports = {
handleProcessExit,
};
18 changes: 9 additions & 9 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@ const CJSLoader = require('internal/modules/cjs/loader');
const { Module, toRealPath, readPackageScope } = CJSLoader;
const { getOptionValue } = require('internal/options');
const path = require('path');
const {
handleProcessExit,
} = require('internal/modules/esm/handle_process_exit');

function resolveMainPath(main) {
// Note extension resolution for the main entry point can be deprecated in a
@@ -51,16 +54,13 @@ function runMainESM(mainPath) {
}));
}

function handleMainPromise(promise) {
// Handle a Promise from running code that potentially does Top-Level Await.
// In that case, it makes sense to set the exit code to a specific non-zero
// value if the main code never finishes running.
function handler() {
if (process.exitCode === undefined)
process.exitCode = 13;
async function handleMainPromise(promise) {
process.on('exit', handleProcessExit);
try {
return await promise;
} finally {
process.off('exit', handleProcessExit);
}
process.on('exit', handler);
return PromisePrototypeFinally(promise, () => process.off('exit', handler));
}

// For backwards compatibility, we have to run a bunch of
6 changes: 6 additions & 0 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
@@ -36,6 +36,10 @@ const {
const format = require('internal/util/inspect').format;
const constants = internalBinding('constants').os.signals;

const {
handleProcessExit,
} = require('internal/modules/esm/handle_process_exit');

function assert(x, msg) {
if (!x) throw new ERR_ASSERTION(msg || 'assertion error');
}
@@ -165,6 +169,8 @@ function wrapProcessMethods(binding) {
memoryUsage.rss = rss;

function exit(code) {
process.off('exit', handleProcessExit);

if (code || code === 0)
process.exitCode = code;

18 changes: 18 additions & 0 deletions test/es-module/test-esm-tla-unfinished.mjs
Original file line number Diff line number Diff line change
@@ -80,3 +80,21 @@ import fixtures from '../common/fixtures.js';
assert.deepStrictEqual([status, stdout], [1, '']);
assert.match(stderr, /Error: Xyz/);
}

{
// Calling process.exit() in .mjs should return status 0
const { status, stdout, stderr } = child_process.spawnSync(
process.execPath,
[fixtures.path('es-modules/tla/process-exit.mjs')],
{ encoding: 'utf8' });
assert.deepStrictEqual([status, stdout, stderr], [0, '', '']);
}

{
// Calling process.exit() in worker thread shouldn't influence main thread
const { status, stdout, stderr } = child_process.spawnSync(
process.execPath,
[fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs')],
{ encoding: 'utf8' });
assert.deepStrictEqual([status, stdout, stderr], [13, '', '']);
}
1 change: 1 addition & 0 deletions test/fixtures/es-modules/tla/process-exit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.exit();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Worker, isMainThread } from 'worker_threads';

if (isMainThread) {
new Worker(new URL(import.meta.url));
await new Promise(() => {});
} else {
process.exit();
}
1 change: 1 addition & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ const expectedModules = new Set([
'NativeModule internal/modules/esm/resolve',
'NativeModule internal/modules/esm/transform_source',
'NativeModule internal/modules/esm/translators',
'NativeModule internal/modules/esm/handle_process_exit',
'NativeModule internal/process/esm_loader',
'NativeModule internal/options',
'NativeModule internal/priority_queue',