即使 process.on(uncaughtException),Nodejs 进程也会崩溃
Nodejs process crashing even with process.on(uncaughtException)
我正在使用 openode.io 来托管 nodejs 应用程序。我正在 运行 上安装一个多人游戏服务器。有时,由于延迟或用户断开连接,进程会发出未捕获的错误并崩溃。我已经修复了其中的一些错误,但在任何时候,如果用户发送无效数据,我的进程就会崩溃。我研究了如何防止这种情况并让 Sentry 记录错误并在 nodejs 进程开始时使用以下代码来防止崩溃:
// prevent crashes
process.on('unhandledRejection', (reason, p) => {
logger.info(reason + ' => Unhandled Rejection at Promise: ' + p);
logger.info('!!!FATAL!!!')
})
process.on('uncaughtException', err => {
logger.info(err + ' => Uncaught Exception thrown');
logger.info('!!!FATAL!!!');
});
我决定使用服务器上发生的常见错误进行测试 运行。
TypeError: Cannot set property 'canTakeThermalDamage' of undefined
File "/opt/app/server.js", line 579, col 45, in Timeout._onTimeout
host.pt[l].canTakeThermalDamage = true;
File "internal/timers.js", line 559, col 11, in listOnTimeout
File "internal/timers.js", line 500, col 7, in processTimers
这个错误不应该导致进程崩溃,但它确实发生了。由于这种情况发生在多人游戏服务器上,因此服务器上的每个用户都会断开连接并必须重新登录。
以下是有关错误的哨兵日志:
当出现这个错误时,process.on运行s里面的代码,但是进程还是崩溃了? Sentry 是否因为发现错误而崩溃?错误是由setTimeout引起的吗?还是另有原因?
无论如何,进程都会退出。它只是为您提供了一些清理选项。
By default, Node.js handles such exceptions by printing the stack trace to stderr and exiting with code 1
当您添加自己的处理程序时(这是您的情况):
Alternatively, change the process.exitCode in the 'uncaughtException' handler which will result in the process exiting with the provided exit code. Otherwise, in the presence of such handler the process will exit with 0.
You can register a listener for unhandledRejection
to do some cleanup before exiting.
- 您应该修复您的代码,使其不抛出异常或使用 try catch 正确处理异常
- 如果你想让
uncaughtException
处理程序总是用 process.exit(1)
退出进程,除非你有很好的理由不这样做
我正在使用 openode.io 来托管 nodejs 应用程序。我正在 运行 上安装一个多人游戏服务器。有时,由于延迟或用户断开连接,进程会发出未捕获的错误并崩溃。我已经修复了其中的一些错误,但在任何时候,如果用户发送无效数据,我的进程就会崩溃。我研究了如何防止这种情况并让 Sentry 记录错误并在 nodejs 进程开始时使用以下代码来防止崩溃:
// prevent crashes
process.on('unhandledRejection', (reason, p) => {
logger.info(reason + ' => Unhandled Rejection at Promise: ' + p);
logger.info('!!!FATAL!!!')
})
process.on('uncaughtException', err => {
logger.info(err + ' => Uncaught Exception thrown');
logger.info('!!!FATAL!!!');
});
我决定使用服务器上发生的常见错误进行测试 运行。
TypeError: Cannot set property 'canTakeThermalDamage' of undefined
File "/opt/app/server.js", line 579, col 45, in Timeout._onTimeout
host.pt[l].canTakeThermalDamage = true;
File "internal/timers.js", line 559, col 11, in listOnTimeout
File "internal/timers.js", line 500, col 7, in processTimers
这个错误不应该导致进程崩溃,但它确实发生了。由于这种情况发生在多人游戏服务器上,因此服务器上的每个用户都会断开连接并必须重新登录。
以下是有关错误的哨兵日志:
无论如何,进程都会退出。它只是为您提供了一些清理选项。
By default, Node.js handles such exceptions by printing the stack trace to stderr and exiting with code 1
当您添加自己的处理程序时(这是您的情况):
Alternatively, change the process.exitCode in the 'uncaughtException' handler which will result in the process exiting with the provided exit code. Otherwise, in the presence of such handler the process will exit with 0. You can register a listener for
unhandledRejection
to do some cleanup before exiting.
- 您应该修复您的代码,使其不抛出异常或使用 try catch 正确处理异常
- 如果你想让
uncaughtException
处理程序总是用process.exit(1)
退出进程,除非你有很好的理由不这样做