Skip to content

Commit 2571c08

Browse files
committed
module: process.exit() should result in exit code 0
1 parent 48e7840 commit 2571c08

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
function handleProcessExit() {
4+
if (process.exitCode === undefined)
5+
process.exitCode = 13;
6+
}
7+
8+
module.exports = {
9+
handleProcessExit,
10+
}

lib/internal/modules/run_main.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const CJSLoader = require('internal/modules/cjs/loader');
88
const { Module, toRealPath, readPackageScope } = CJSLoader;
99
const { getOptionValue } = require('internal/options');
1010
const path = require('path');
11+
const { handleProcessExit } = require('internal/modules/esm/handle_process_exit');
1112

1213
function resolveMainPath(main) {
1314
// Note extension resolution for the main entry point can be deprecated in a
@@ -56,15 +57,11 @@ async function handleMainPromise(promise) {
5657
// Handle a Promise from running code that potentially does Top-Level Await.
5758
// In that case, it makes sense to set the exit code to a specific non-zero
5859
// value if the main code never finishes running.
59-
function handler() {
60-
if (process.exitCode === undefined)
61-
process.exitCode = 13;
62-
}
63-
process.on('exit', handler);
60+
process.on('exit', handleProcessExit);
6461
try {
6562
return await promise;
6663
} finally {
67-
process.off('exit', handler);
64+
process.off('exit', handleProcessExit);
6865
}
6966
}
7067

lib/internal/process/per_thread.js

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const {
4848
} = require('internal/validators');
4949
const constants = internalBinding('constants').os.signals;
5050

51+
const { handleProcessExit } = require('internal/modules/esm/handle_process_exit');
52+
5153
const kInternal = Symbol('internal properties');
5254

5355
function assert(x, msg) {
@@ -175,6 +177,8 @@ function wrapProcessMethods(binding) {
175177
memoryUsage.rss = rss;
176178

177179
function exit(code) {
180+
process.off('exit', handleProcessExit);
181+
178182
if (code || code === 0)
179183
process.exitCode = code;
180184

test/es-module/test-esm-tla-unfinished.mjs

+19
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,22 @@ import fixtures from '../common/fixtures.js';
8080
assert.deepStrictEqual([status, stdout], [1, '']);
8181
assert.match(stderr, /Error: Xyz/);
8282
}
83+
84+
{
85+
// calling process.exit() in .mjs should return status 0
86+
const { status, stdout, stderr } = child_process.spawnSync(
87+
process.execPath,
88+
[fixtures.path('es-modules/tla/process-exit.mjs')],
89+
{ encoding: 'utf8' });
90+
assert.deepStrictEqual([status, stdout, stderr], [0, '', '']);
91+
}
92+
93+
{
94+
// calling process.exit() in .mjs should return status 0
95+
const { status, stdout, stderr } = child_process.spawnSync(
96+
process.execPath,
97+
[fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs')],
98+
{ encoding: 'utf8' });
99+
assert.deepStrictEqual([status, stdout, stderr], [13, '', '']);
100+
}
101+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.exit();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Worker } from 'worker_threads';
2+
3+
// Do not use isMainThread so that this test itself can be run inside a Worker.
4+
if (!process.env.HAS_STARTED_WORKER) {
5+
process.env.HAS_STARTED_WORKER = 1;
6+
new Worker(import.meta.url.slice('file://'.length));
7+
await new Promise(() => {});
8+
} else {
9+
process.exit()
10+
}

0 commit comments

Comments
 (0)