Skip to content

Commit

Permalink
module: do not use process.exit()
Browse files Browse the repository at this point in the history
Inside workers, using stdio is always asynchronous, so using
`process.exit()` always interrupts sending of messages to the
parent thread, including error messages presented over stdio.

Do not use `process.exit()` and instead trigger a “real”
uncaught exception.

PR-URL: #25769
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Jan 30, 2019
1 parent 94ebf3f commit 7fa5157
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 1 addition & 5 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ module.exports = Module;
let asyncESM;
let ModuleJob;
let createDynamicModule;
let decorateErrorStack;

function lazyLoadESM() {
asyncESM = require('internal/process/esm_loader');
ModuleJob = require('internal/modules/esm/module_job');
createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
decorateErrorStack = require('internal/util').decorateErrorStack;
}

const {
Expand Down Expand Up @@ -794,9 +792,7 @@ Module.runMain = function() {
return loader.import(pathToFileURL(process.argv[1]).pathname);
})
.catch((e) => {
decorateErrorStack(e);
console.error(e);
process.exit(1);
internalBinding('util').triggerFatalException(e);
});
} else {
Module._load(process.argv[1], null, true);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-worker-esm-missing-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');

const worker = new Worker('./does-not-exist.js', {
execArgv: ['--experimental-modules'],
});

worker.on('error', common.mustCall((err) => {
// eslint-disable-next-line node-core/no-unescaped-regexp-dot
assert(/Cannot find module .+does-not-exist.js/.test(err.message), err);
}));

0 comments on commit 7fa5157

Please sign in to comment.