简体   繁体   English

防止崩溃进程发生堆栈溢出异常

[英]Prevent Stack Overflow Exception from crashing process

Assume I have an application, where user can provide script written in JavaScript to perform some task in it. 假设我有一个应用程序,用户可以在其中提供用JavaScript编写的脚本来执行某些任务。 This is done using Jint. 这是使用Jint完成的。 However, badly written script can cause Stack Overflow Exception in JintEngine.Run() and therefore crashing whole application. 但是,编写错误的脚本会导致JintEngine.Run()JintEngine.Run() Stack Overflow异常,从而导致整个应用程序崩溃。 I would rather like to inform user about the error in script. 我宁愿告诉用户脚本中的错误。

I tried to run Jint on another application domain, that did not help, cause AFAIK default action on SOE is exiting process. 我试图在另一个应用程序域上运行Jint,这没有帮助,导致SOE上的AFAIK默认操作正在退出进程。 Could be it changed any other way than by using CLR hosting? 可以通过使用CLR托管来改变任何其他方式吗?

I know I can run separate process and that's my fallback, nevertheless I would like not to do that. 我知道我可以运行单独的流程,这是我的后备,但我不想这样做。

As you suspected, it's just not possible to catch a StackOverflowException generated by the runtime unless you're hosting the CLR yourself [1]. 正如您所怀疑的那样,除非您自己托管CLR,否则捕获运行时生成的StackOverflowException是不可能的[1]。 As the Microsoft documentation suggests, your best bet is to try and detect the stack overflow before it happens. 正如Microsoft文档所示,最好的办法是在堆栈溢出发生之前尝试检测它。

In the case of using Jint, this means finding some way to hook into the execution engine and attach your own code to arbitrary method calls. 在使用Jint的情况下,这意味着找到一些方法来挂钩执行引擎并将您自己的代码附加到任意方法调用。 Fortunately, a brief overview of their documentation reveals that they have a debugging mode that does just that! 幸运的是,他们的文档的简要概述表明他们有一个调试模式就是这样! [2] [2]

No doubt running in debugging mode means slowing down your script execution; 毫无疑问,在调试模式下运行意味着减慢脚本执行速度; it's up to you to decide if the benefit of detecting overflows is worth the penalty--and I definitely suggest measuring that penalty to ensure it's really going to be a problem. 由你来决定检测溢出的好处是否值得惩罚 - 我肯定建议测量这个惩罚以确保它真的会成为一个问题。 One possible mitigation would be to allow the user to choose when debugging mode is active. 一种可能的缓解方法是允许用户选择何时调试模式处于活动状态。

-Mark -标记

[1] http://msdn.microsoft.com/en-us/library/system.stackoverflowexception.aspx (see remarks) [1] http://msdn.microsoft.com/en-us/library/system.stackoverflowexception.aspx (见备注)

[2] http://jint.codeplex.com/wikipage?title=Debugging&referringTitle=Documentation [2] http://jint.codeplex.com/wikipage?title=Debugging&referringTitle=Documentation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM