Skip to content

weird behaviour from uncaughtExceptionMonitor for unhandled rejections #35291

Description

@mlarcher

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 🤷

Activity

  1. changed the title [-]weird behaviour from uncaughtExceptionMonitor[/-] [+]weird behaviour from uncaughtExceptionMonitor for unhandled rejections[/+] on Sep 22, 2020
  2. BridgeAR commented on Sep 22, 2020

    @BridgeAR
    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.

  3. added
    processIssues and PRs related to the process subsystem.
    questionIssues asking questions about Node.js.
    on Sep 22, 2020
  4. mlarcher commented on Sep 22, 2020

    @mlarcher
    ContributorAuthor

    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

  5. BridgeAR commented on Sep 22, 2020

    @BridgeAR
    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 :-)

  6. mlarcher commented on Sep 22, 2020

    @mlarcher
    ContributorAuthor

    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.

  7. mlarcher commented on Sep 22, 2020

    @mlarcher
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    processIssues and PRs related to the process subsystem.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions