为什么异常会导致 Node.js 中的资源泄漏? [英] Why would an exception cause resource leaks in Node.js?

查看:20
本文介绍了为什么异常会导致 Node.js 中的资源泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您查看 Node.js 域文档 的开头,它指出:

If you look at the beginning of the Node.js documentation for domains it states:

根据 JavaScript 中 throw 工作原理的本质,几乎没有任何方法可以安全地从上次中断的地方继续",而不会泄漏引用或创建其他类型的未定义脆弱状态.

By the very nature of how throw works in JavaScript, there is almost never any way to safely "pick up where you left off", without leaking references, or creating some other sort of undefined brittle state.

再次在第一部分给出的代码示例中,它说:

Again in the code example it gives in that first section it says:

虽然我们阻止了进程突然重启,但我们正在疯狂地泄漏资源

Though we've prevented abrupt process restarting, we are leaking resources like crazy

我想了解为什么会这样?什么资源泄露?他们建议您仅使用域来捕获错误并安全地关闭进程.这是所有例外的问题,而不仅仅是在使用域时?在 Javascript 中抛出和捕获异常是一种不好的做法吗?我知道这是 Python 中的常见模式.

I would like to understand why this is the case? What resources are leaking? They recommend that you only use domains to catch errors and safely shutdown a process. Is this a problem with all exceptions, not just when working with domains? Is it a bad practice to throw and catch exceptions in Javascript? I know it's a common pattern in Python.

编辑

如果抛出异常,我可以理解为什么在非垃圾收集语言中可能会出现资源泄漏,因为如果抛出异常,您可能运行以清理对象的任何代码都不会运行.

I can understand why there could be resource leaks in a non garbage collected language if you throw an exception because then any code you might run to clean up objects wouldn't run if an exception is thrown.

我可以用 Javascript 想象的唯一原因是,如果抛出异常会在抛出异常的范围内存储对变量的引用(可能还有调用堆栈中的内容),从而保持引用,然后保留异常对象周围,​​永远不会被清理.除非所指的泄漏资源是引擎内部的资源.

The only reason I can imagine with Javascript is if throwing an exception stores references to variables in the scope where the exception was thrown (and maybe things in the call stack), thus keeping references around, and then the exception object is kept around and never gets cleaned up. Unless the leaking resources referred to are resources internal to the engine.

更新

我现在写了一篇博客更好地解释了这个问题的答案.检查一下

I've Written a blog explaining the answer to this a bit better now. Check it out

推荐答案

意外的异常是您需要担心的.如果您对应用程序的状态了解不足,无法为特定异常添加处理并管理任何必要的状态清理,那么根据定义,您的应用程序的状态是未定义且不可知的,并且很可能存在某些情况闲逛不应该.您需要担心的不仅仅是内存泄漏.未知的应用程序状态可能会导致不可预测和不需要的应用程序行为(例如提供错误的输出——部分渲染的模板,或不完整的计算结果,或更糟糕的是,每个后续输出都是错误的情况).这就是为什么在发生未处理的异常时退出进程很重要的原因.它让您的应用有机会自我修复.

Unexpected exceptions are the ones you need to worry about. If you don't know enough about the state of the app to add handling for a particular exception and manage any necessary state cleanup, then by definition, the state of your app is undefined, and unknowable, and it's quite possible that there are things hanging around that shouldn't be. It's not just memory leaks you have to worry about. Unknown application state can cause unpredictable and unwanted application behavior (like delivering output that's just wrong -- a partially rendered template, or an incomplete calculation result, or worse, a condition where every subsequent output is wrong). That's why it's important to exit the process when an unhandled exception occurs. It gives your app the chance to repair itself.

发生异常,这很好.拥抱它.关闭进程并使用诸如 Forever 之类的东西来检测它并使事情回到正轨.集群和域也很棒.您正在阅读的文本并不是警告您不要抛出异常,也不是在您处理了预期的异常后继续进程——而是在发生意外异常时防止进程继续运行.

Exceptions happen, and that's fine. Embrace it. Shut down the process and use something like Forever to detect it and set things back on track. Clusters and domains are great, too. The text you were reading is not a caution against throwing exceptions, or continuing the process when you've handled an exception that you were expecting -- it's a caution against keeping the process running when unexpected exceptions occur.

这篇关于为什么异常会导致 Node.js 中的资源泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆