pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

PM2 always logs unhandledRejection and uncaughtException to error stream

Open Khez opened this issue 4 years ago • 3 comments

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:

  • ForkMode: https://github.com/Unitech/pm2/issues/3644#issuecomment-435598195
  • ClusterMode: https://github.com/Unitech/pm2/issues/3644#issuecomment-438922710

Pull Request

  • Do we at least add an identifiable message to both scenarios? Something along the lines of PM2 Warning: ${error}
  • Do we add a new option logExceptions default true for backwards compatibility ?
  • Do we check number of handlers and not log if there's more than one added ? Similar to the check for process.exit:
    • https://github.com/Unitech/pm2/blob/4.5.0/lib/ProcessContainer.js#L283
    • https://github.com/keymetrics/pm2-io-apm/blob/4.3.5/src/features/notify.ts#L157

Khez avatar Nov 10 '20 19:11 Khez

ping @Unitech

Khez avatar Jan 14 '21 12:01 Khez

Is there any further information about this, or is this considered a dead proposal?

mitjafelicijan avatar Mar 25 '22 03:03 mitjafelicijan

We've also noticed this in our log files. It's especially annoying because we use JSON log types and when these rejections come through, the output is not valid JSON.

@Unitech do you have any input on this?

jstrese avatar Jul 05 '22 18:07 jstrese