Skip to content

Fix #15: process 'exit' listeners should run before fatal error is logged #16

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

Merged
merged 4 commits into from
Jul 21, 2021
Merged
Changes from all commits
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
23 changes: 12 additions & 11 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function getErrorSource(error) {
return null;
}

function printErrorAndExit (error) {
function printFatalErrorUponExit (error) {
var source = getErrorSource(error);

// Ensure error is printed synchronously and not truncated
Expand All @@ -505,23 +505,24 @@ function printErrorAndExit (error) {
colors: process.stderr.isTTY
})
);
process.exit(1);
}

function shimEmitUncaughtException () {
var origEmit = process.emit;
var isTerminatingDueToFatalException = false;
var fatalException;

process.emit = function (type) {
if (type === 'uncaughtException') {
var hasStack = (arguments[1] && arguments[1].stack);
var hasListeners = (this.listeners(type).length > 0);

if (hasStack && !hasListeners) {
return printErrorAndExit(arguments[1]);
}
const hadListeners = origEmit.apply(this, arguments);
if (type === 'uncaughtException' && !hadListeners) {
isTerminatingDueToFatalException = true;
fatalException = arguments[1];
process.exit(1);
}

return origEmit.apply(this, arguments);
if (type === 'exit' && isTerminatingDueToFatalException) {
printFatalErrorUponExit(fatalException);
}
return hadListeners;
};
}

Expand Down