Skip to content

Commit

Permalink
module: process.exit() should result in exit code 0
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonBall committed Jan 3, 2022
1 parent 48e7840 commit 2571c08
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/internal/modules/esm/handle_process_exit.js
@@ -0,0 +1,10 @@
'use strict';

function handleProcessExit() {
if (process.exitCode === undefined)
process.exitCode = 13;
}

module.exports = {
handleProcessExit,
}
9 changes: 3 additions & 6 deletions lib/internal/modules/run_main.js
Expand Up @@ -8,6 +8,7 @@ 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
Expand Down Expand Up @@ -56,15 +57,11 @@ async 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;
}
process.on('exit', handler);
process.on('exit', handleProcessExit);
try {
return await promise;
} finally {
process.off('exit', handler);
process.off('exit', handleProcessExit);
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/internal/process/per_thread.js
Expand Up @@ -48,6 +48,8 @@ const {
} = require('internal/validators');
const constants = internalBinding('constants').os.signals;

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

const kInternal = Symbol('internal properties');

function assert(x, msg) {
Expand Down Expand Up @@ -175,6 +177,8 @@ function wrapProcessMethods(binding) {
memoryUsage.rss = rss;

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

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

Expand Down
19 changes: 19 additions & 0 deletions test/es-module/test-esm-tla-unfinished.mjs
Expand Up @@ -80,3 +80,22 @@ 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 .mjs should return status 0
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
@@ -0,0 +1 @@
process.exit();
@@ -0,0 +1,10 @@
import { Worker } from 'worker_threads';

// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
new Worker(import.meta.url.slice('file://'.length));
await new Promise(() => {});
} else {
process.exit()
}

0 comments on commit 2571c08

Please sign in to comment.