Skip to content

Commit

Permalink
Sentry Node: do not exit process if the app added its own 'uncaughtEx…
Browse files Browse the repository at this point in the history
…ception' or 'unhandledRejection' listener

- Fixes #1661
  • Loading branch information
ibc authored and lforst committed May 30, 2022
1 parent 571d661 commit 27b77a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 7 additions & 4 deletions packages/node/src/integrations/onuncaughtexception.ts
Expand Up @@ -58,6 +58,9 @@ export class OnUncaughtException implements Integration {
return (error: Error): void => {
let onFatalError: OnFatalErrorHandler = logAndExitProcess;
const client = getCurrentHub().getClient<NodeClient>();
// in order to honour Node's original behavior on uncaught exceptions, we should not
// exit the process if the app added its own 'uncaughtException' listener
const shouldExitProcess: boolean = global.process.listenerCount('uncaughtException') === 1;

if (this._options.onFatalError) {
// eslint-disable-next-line @typescript-eslint/unbound-method
Expand All @@ -83,23 +86,23 @@ export class OnUncaughtException implements Integration {
originalException: error,
data: { mechanism: { handled: false, type: 'onuncaughtexception' } },
});
if (!calledFatalError) {
if (!calledFatalError && shouldExitProcess) {
calledFatalError = true;
onFatalError(error);
}
});
} else {
if (!calledFatalError) {
if (!calledFatalError && shouldExitProcess) {
calledFatalError = true;
onFatalError(error);
}
}
} else if (calledFatalError) {
} else if (calledFatalError && shouldExitProcess) {
// we hit an error *after* calling onFatalError - pretty boned at this point, just shut it down
IS_DEBUG_BUILD &&
logger.warn('uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown');
logAndExitProcess(error);
} else if (!caughtSecondError) {
} else if (!caughtSecondError && shouldExitProcess) {
// two cases for how we can hit this branch:
// - capturing of first error blew up and we just caught the exception from that
// - quit trying to capture, proceed with shutdown
Expand Down
8 changes: 7 additions & 1 deletion packages/node/src/integrations/onunhandledrejection.ts
Expand Up @@ -70,6 +70,10 @@ export class OnUnhandledRejection implements Integration {
'or by rejecting a promise which was not handled with .catch().' +
' The promise rejected with the reason:';

// in order to honour Node's original behavior on unhandled rejections, we should not
// exit the process if the app added its own 'unhandledRejection' listener
const shouldExitProcess: boolean = global.process.listenerCount('unhandledRejection') === 1;

/* eslint-disable no-console */
if (this._options.mode === 'warn') {
consoleSandbox(() => {
Expand All @@ -81,7 +85,9 @@ export class OnUnhandledRejection implements Integration {
consoleSandbox(() => {
console.warn(rejectionWarning);
});
logAndExitProcess(reason);
if (shouldExitProcess) {
logAndExitProcess(reason);
}
}
/* eslint-enable no-console */
}
Expand Down

0 comments on commit 27b77a4

Please sign in to comment.