Process doesn't crash on unhandledRejection when it is run via PM2
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?
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior with the provided test.mjs under Node.js 16 and PM2 5.2.0, then inspect the suspected handler in pm2-io-apm at src/features/notify.ts around line 191. Compare PM2's handling with direct Node.js execution and verify that the default unhandled-rejection behavior and expected process exit are preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- devops
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100