Question: I use lua interfaces to get lua support in my C# program, the worker thread will freeze if the user submits code like this

```
while true do end

```

I have a way to detect if a infinite loop is running, but I need a nice way of exiting the DoString method from the Worker thread. Any ideas?

edit: @kikito, Yes Im deteting it something like that. The problem I have is that I cant find a clean way of killing the DoString method, it looks like the Lua interfaces main class (Lua) has some static dependencies, because if I do `lua.Close();` on my instance it will abort the DoString method, but the next time I instance an lua class `new Lua();` it will crash saying something about protective memory

edit: A feature branch showing my .Close code
<https://github.com/AndersMalmgren/FreePIE/tree/detect-and-recover-infite-lua-loop>

Answer: 
Sandboxing Lua
--------------

Setting hooks is not sufficient *at all* to prevent unintended waste of resources, let alone abuse- here's a simple example (the time is spent during string pattern matching- no debug hooks get called):

`s=('a'):rep(20000):match('.-b')`

**The only reliable way to force time/memory constraints on a piece of Lua code is to run the Lua interpreter in a process of its own and to make your OS monitor that process.**

The nice thing with Lua is that you won't need any complicated, OS-dependent permission setup for sandboxing: you just limit time and memory (reasonable; on windows there are [Job Objects](http://msdn.microsoft.com/en-us/library/ms684161(v=vs.85).aspx), Unix has ulimits- relevant: [Linux resource limitation](http://coldattic.info/shvedsky/pro/blogs/a-foo-walks-into-a-bar/posts/40)) and then keep things like os.execute, half the io-library and modules like luasocket away from the scripter (pretty easy).

Recovering from errors in sandboxed code
----------------------------------------

You can handle almost everything (except violation of time/memory limits) without trashing your Lua interpreter: Just wrap the execution of user-supplied code inside a [pcall](http://www.lua.org/manual/5.1/manual.html#lua_pcall); if you call any Lua-API functions that might fail yourself, you need to wrap them inside a function that you can pcall, too (or set a Lua panic function and handle it from there).

---

[I didn't want people glancing at this thread to assume that debug.sethook is adequate for sandboxing, and stackoverflow would not let me comment (yet)]
What about setting line hook *before* running lua code? I don't know is it possible in C#, but it is possible in original C lua.

Is yor loop detector already run in different C# task?

* If yes, use C# variable with C# hook. In this case your loop detector set and hool check C# variable like `terminateLua` and throw lua error (it should be possible).
* If no, and you detect loop on lua side (also using hooks?), set lua variable (make it upvalue to prevent user tricks) and also throw error.

Important part is to don't reset this (either C# or lua) variable before you are really sure about finishing your task, because user can catch your error via `pcall` and try to handle it.