Unitech / Unitech/pm2

PM2 always logs unhandledRejection and uncaughtException to error stream

Open
#4,913 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

What's going wrong?

Error stream is polluted with an intrusive log that can't be turned off or customised.

PM2 in cluster mode does logError(error) in https://github.com/Unitech/pm2/blob/4.5.0/lib/ProcessContainer.js#L258
PM2 in fork mode with module @pm2/io does console.error(error) in https://github.com/keymetrics/pm2-io-apm/blob/4.3.5/src/features/notify.ts#L166

This happens even if a custom uncaughtException or unhandledRejection is added through the app.

How could we reproduce this issue?

Create an app.js file:

process.on('unhandledRejection', (error) => {
    console.error(JSON.stringify({
        date: new Date(),
        message: error.message || error
    }));
});

const rejected = Promise.reject('Example Unhandled Rejection');

Running this through node app.js outputs on console.error our custom JSON error message:

{"date":"2020-11-10T14:58:28.221Z","message":"Example Unhandled Rejection"}

Running this through pm2 start app.js (fork mode) breaks our JSON error message:

Example Unhandled Rejection
{"date":"2020-11-10T14:58:28.221Z","message":"Example Unhandled Rejection"}

Running this through pm2 start app.js -i 1 (cluster mode) breaks our JSON error message:

You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
Example Unhandled Rejection
{"date":"2020-11-10T14:58:28.221Z","message":"Example Unhandled Rejection"}

Note This gets compounded as the error stack grows larger.

Supporting information

I believe this is the same issue discussed in #3644 and #4298 and was attempted to be handled by https://github.com/keymetrics/pm2-io-apm/issues/127 / https://github.com/keymetrics/pm2-io-apm/pull/128

The problem with both fork and cluster mode is mentioned in:

Pull Request

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/ProcessContainer.js at the cited logError call and the @pm2/io-apm src/features/notify.ts handler, then reproduce the app in fork and cluster modes. Compare the existing behavior with the custom unhandledRejection handler and resolve the intended logging or configuration behavior; done means the unwanted duplicate output is addressed in both scenarios.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.