Hello!
On Mon, May 5, 2014 at 11:10 AM, kikito wrote:
> I have a web application where the end user is able to input certain Lua
> code to be executed.
>
> Code coming from users is heavily sandboxed - they can't touch anything
> dangerous. However, they can halt the worker they are in if they try to
> execute an infinite loop (or a similarly lengthy operation):
>
> while true do end
>
The ngx_lua module has not yet provided a built-in mechanism for
automatically aborting such infinite loop with a timeout or some other
threshold. Along this path, we need to use a custom OS thread to call
lua_sethook() to add a hook with a count of 1 to abort the too hot Lua
thread (in the main OS thread of nginx) and abandon it. And we need to
enable -DLUAJIT_ENABLE_CHECKHOOK for the LuaJIT build too. See Mike
Pall's explanation here:
http://www.freelists.org/post/luajit/Efficient-query-timeouts-in-LuaJIT,1
Just as Mike mentioned in the mail above, a much more efficient (and
simpler) way is to use a higher level DSL for your user input rather
than direct Lua code (or you can parse the Lua code yourself) and
check the threshold in the resulting Lua code to be run directly by
the LuaJIT VM. The lua_sethook() thing can only serve as the last
resort.
Anyway, you're welcome to submit patches for the lua_sethook() thing
if you decide to go that path.
Regards,
-agentzh