pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Process doesn't crash on unhandledRejection when it is run via PM2

Open RussCoder opened this issue 3 years ago • 4 comments

What's going wrong?

When a process is started via PM2, it seems that global error handler uncaughtException doesn't work for unhandled rejections. Also, the process doesn't crash too. But if a process is started directly via Node.js 16, it crashes as expected.

How could we reproduce this issue?

Create a file test.mjs with the following content:

import { setTimeout } from 'node:timers/promises';

process.on('uncaughtException', (e, origin) => {
    console.info('Uncaught exception. The server will exit. Origin is ' + origin);
    console.error(e);
    process.exit(1);
});

const time = 2000;
console.log(`Crash in ${time} ms`);

(async () => {
    await setTimeout(time);
    console.log(process.something.something);
})();

setInterval(() => {
    console.log('Working...');
}, 1000);

Then run it with Node.js 16: node test.mjs. It will crash, the error handler will work.

Now run it via pm2 5.2.0: pm2 start test.mjs In the logs (pm2 logs --lines 100) you will see the error, but the handler will not work, the process will not exit.

If you remove the uncaughtException handler, the result will be the same: process crashes in the first case, and it continues in the second.

Supporting information

local pm2: 5.2.0
pm2d version:  5.2.0
node version: 16.16.0
OS: Windows 10 

A workaround is to add

process.on('unhandledRejection', e => {
    console.log('Unhandled rejection. The server will exit.');
    console.error(e);
    process.exit(1);
})

However, it's confusing. The default behavior of Node.js is to exit. What's more, in case of ESM modules with top level await, such a handler must be added in the first imported module that itself has no imports - it will be executed first. Otherwise, if you add the handler after all imports in the application entry point file and if an error occurs in a module with a top-level await, the process will not crash, because the handler will not be set.

Probable solution

It seems that PM2 has an unhandledRejection handler, so unhandled rejections are treated as handled. I suppose this is it: https://github.com/keymetrics/pm2-io-apm/blob/b6b1bd776c8b147a396be1c095766d908d05775a/src/features/notify.ts#L191

The uncaughtExceptionMonitor event should be used instead to collect errors, but not change the default Node.js' behavior.

RussCoder avatar Jul 27 '22 10:07 RussCoder

@Unitech please, take a look. I even added a probable solution. It seems to be a serious problem that is rather easy to fix for a person who knows the code. I can try to create a PR - but that repo seems to have a lot of functions, and this error collection isn't in the free pm2 version. So it would be nice if one of the main maintainers solve it.

RussCoder avatar Jul 27 '22 15:07 RussCoder

This issue is now a year old but can confirm that this is still happening and a very very serious problem in PM2. I'll probably make a fork that doesn't do this.

azuliani avatar Sep 29 '23 14:09 azuliani

  1. So any alternative to PM2? ESM support is also driving me away from it.

vilicvane avatar Jan 07 '24 09:01 vilicvane