Skip to content
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

weird behaviour from uncaughtExceptionMonitor for unhandled rejections #35291

Closed
mlarcher opened this issue Sep 22, 2020 · 5 comments
Closed
Labels
process Issues and PRs related to the process subsystem. question Issues that look for answers.

Comments

@mlarcher
Copy link
Contributor

mlarcher commented Sep 22, 2020

What steps will reproduce the bug?

process.on('uncaughtExceptionMonitor', (err, origin) => {
  console.log('uncaughtExceptionMonitor handler', origin);
});

async function uncaughtPromise() {
  throw new Error('yo');
}

uncaughtPromise();

What is the expected behavior?

I should see the uncaughtExceptionMonitor handler

What do you see instead?

(node:39) UnhandledPromiseRejectionWarning: Error: yo
    at uncaughtPromise (/data/tmp.js:6:9)
    at Object.<anonymous> (/data/tmp.js:9:1)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:39) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:39) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Additional information

If I add a handler for unhandledRejection that just re-throws the error like so:

process.on('unhandledRejection', (err) => {
  console.log('unhandledRejection handler‘)
  throw err;
});

process.on('uncaughtExceptionMonitor', (err, origin) => {
  console.log('uncaughtExceptionMonitor handler', origin);
});

async function uncaughtPromise() {
  throw new Error('yo');
}

uncaughtPromise();

then the uncaughtExceptionMonitor is reached :

unhandledRejection handler
uncaughtExceptionMonitor handler uncaughtException
/data/tmp.js:3
  throw err;
  ^

Error: yo
    at uncaughtPromise (/data/tmp.js:11:9)
    at Object.<anonymous> (/data/tmp.js:14:1)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

(but that is now an uncaughtException and not an unhandledRejection anymore, when I come to think about it !)

Also, an exception not related to unhandled promise works as expected, i.e.:

process.on('uncaughtExceptionMonitor', (err, origin) => {
  console.log('uncaughtExceptionMonitor handler', origin);
});

unknownFunction();

gives me the expected

uncaughtExceptionMonitor handler uncaughtException
/data/tmp.js:5
unknownFunction();
^

ReferenceError: unknownFunction is not defined
    at Object.<anonymous> (/data/tmp.js:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

So basically it seems to me that uncaughtExceptionMonitor doesn't work for unhandledRejection 🤷

@mlarcher mlarcher changed the title weird behaviour from uncaughtExceptionMonitor weird behaviour from uncaughtExceptionMonitor for unhandled rejections Sep 22, 2020
@BridgeAR
Copy link
Member

This seems to work as intended. The reason is that there's a difference between a promise rejection and an uncaught exception. The monitor only works for exceptions but not for rejections.

@BridgeAR BridgeAR added process Issues and PRs related to the process subsystem. question Issues that look for answers. labels Sep 22, 2020
@mlarcher
Copy link
Contributor Author

mlarcher commented Sep 22, 2020

What is the point of the origin value then ? Docs states that "origin Indicates if the exception originates from an unhandled rejection or from synchronous errors. Can either be 'uncaughtException' or 'unhandledRejection'."
cf https://nodejs.org/api/process.html#process_event_uncaughtexceptionmonitor

@BridgeAR
Copy link
Member

Seems like the documentation should be updated for the exception monitor. It should be identical to the documentation here https://nodejs.org/api/process.html#process_event_uncaughtexception. A PR is welcome :-)

@mlarcher
Copy link
Contributor Author

So basically the handler will work as I expect only if I run node with a --unhandled-rejections flag set to strict... That makes sense and I did confirm it locally.
Thanks for your quick reply, I'll try to make a PR for the doc update.

@mlarcher
Copy link
Contributor Author

Just found out that the issue is basically the same as #32907

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
process Issues and PRs related to the process subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

2 participants