开发者

Stopping a runaway Lua subprocess

I have embedded Lua in an Objective-C application using LuaObjCBridge. I need 开发者_开发技巧to know how to stop the Lua process if it taking too much time (infinite loop?).

Would running it in a separate thread help?


The usual way to do this is to use lua_sethook to schedule a callback every count VM instructions; when the callback lua_Hook function occurs after a excessive time your hook function can raise an error forcing control to your protected call.


Doug's answer already provides the default necessary to restrict normal lua code execution. If you need to limit this for security reasons, you should know that there are known ways to use lua library calls, such as string pattern matching functions, to create practical infinite loops. The instruction count hook won't catch these for you since the lua instruction count is not incrementing while the c function call is executing. For a solution of this calibre, you need OS-level restrictions (separate process, interrupt from SIGALRM?)


For OS-level restrictions like kaizer.se mentions, one good approach for running Lua stand-alone on *nix systems is to use ulimit -t 1 to restrict the Lua process to one second of CPU time. This is the approach the CGI script that powers the live demo on Lua.org uses.

For an application like the one you described, using your environment's thread facilities is the best option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜