Skip to content

Commit b8308de

Browse files
committed
Sentry Node: do not exit process if the app added its own 'uncaughtException' or 'unhandledRejection' listener
- Fixes #1661
1 parent c518955 commit b8308de

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/node/src/integrations/onuncaughtexception.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export class OnUncaughtException implements Integration {
5858
return (error: Error): void => {
5959
let onFatalError: OnFatalErrorHandler = logAndExitProcess;
6060
const client = getCurrentHub().getClient<NodeClient>();
61+
// in order to honour Node's original behavior on uncaught exceptions, we should not
62+
// exit the process if the app added its own 'uncaughtException' listener
63+
const shouldExitProcess: boolean = global.process.listenerCount('uncaughtException') === 1;
6164

6265
if (this._options.onFatalError) {
6366
// eslint-disable-next-line @typescript-eslint/unbound-method
@@ -83,23 +86,23 @@ export class OnUncaughtException implements Integration {
8386
originalException: error,
8487
data: { mechanism: { handled: false, type: 'onuncaughtexception' } },
8588
});
86-
if (!calledFatalError) {
89+
if (!calledFatalError && shouldExitProcess) {
8790
calledFatalError = true;
8891
onFatalError(error);
8992
}
9093
});
9194
} else {
92-
if (!calledFatalError) {
95+
if (!calledFatalError && shouldExitProcess) {
9396
calledFatalError = true;
9497
onFatalError(error);
9598
}
9699
}
97-
} else if (calledFatalError) {
100+
} else if (calledFatalError && shouldExitProcess) {
98101
// we hit an error *after* calling onFatalError - pretty boned at this point, just shut it down
99102
IS_DEBUG_BUILD &&
100103
logger.warn('uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown');
101104
logAndExitProcess(error);
102-
} else if (!caughtSecondError) {
105+
} else if (!caughtSecondError && shouldExitProcess) {
103106
// two cases for how we can hit this branch:
104107
// - capturing of first error blew up and we just caught the exception from that
105108
// - quit trying to capture, proceed with shutdown

packages/node/src/integrations/onunhandledrejection.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ export class OnUnhandledRejection implements Integration {
9191
'or by rejecting a promise which was not handled with .catch().' +
9292
' The promise rejected with the reason:';
9393

94+
// in order to honour Node's original behavior on unhandled rejections, we should not
95+
// exit the process if the app added its own 'unhandledRejection' listener
96+
const shouldExitProcess: boolean = global.process.listenerCount('unhandledRejection') === 1;
97+
9498
/* eslint-disable no-console */
9599
if (this._options.mode === 'warn') {
96100
consoleSandbox(() => {
@@ -102,7 +106,9 @@ export class OnUnhandledRejection implements Integration {
102106
consoleSandbox(() => {
103107
console.warn(rejectionWarning);
104108
});
105-
logAndExitProcess(reason);
109+
if (shouldExitProcess) {
110+
logAndExitProcess(reason);
111+
}
106112
}
107113
/* eslint-enable no-console */
108114
}

0 commit comments

Comments
 (0)