0

Possible Duplicate:
Prevent Lua infinite loop

This is a Lua for C# (Lua interface) specific question, it does not work as in native C Lua, so please if you have no experience from Lua C# think twice if your answer can help me, thanks

So the problem is if a user supplies a script like this

while (true) do end

How the heck do I abort it cleanly without Lua dying on me? I've tried lots of approaches, just calling lua.Close() from a seperate thread will just give you a unprotected error in call to Lua API (attempt to index a nil value) error, the source of the problem is that Lua isnt thread safe, and since I have to call close from a separate thread (The main thread is busy serving the DoString call) its a bit of a moment 22 problem. I tried adding a debug hook and also checked that the Thread ID's are the same between the DoString thread and the hook, and they are the same, ergo anything executed in the hook is executed on the DoString thread. This doesn't help, still getting the exception. How the ... do you abort a DoString in Lua C#!?

Thanks

edit

Gotten a little further, if I dispose the lua class I get a exceptions in thread running the infinite DoString, in debug from VStudio I can just catch this and recover. But if I run the exe either in debug or release I't sometimes cant recover and crashes!? The error i get from the DoString thread is

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Community
  • 1
  • 1
Anders
  • 17,306
  • 10
  • 76
  • 144
  • Which "Lua for C#" are you talking about? – Nicol Bolas Jul 24 '12 at 07:58
  • There are more than one? http://code.google.com/p/luainterface/ – Anders Jul 24 '12 at 08:00
  • Yes, I personally prefer https://github.com/jsimmons/LuaSharp as it runs under Mono. – Pondidum Jul 24 '12 at 08:16
  • Pondidum, does it handle above problem better? – Anders Jul 24 '12 at 08:19
  • Unfortunately not, I had a quick test of it when I read your question. – Pondidum Jul 24 '12 at 15:53
  • Worked some more on this today, found this, looks like you have to flood the lua engine with exceptions to get it to shutdown? http://stackoverflow.com/questions/6913999/forcing-a-lua-script-to-exit The strange thing is that i get the same old Protected memory error if I let the hook fire more than once, I can even have a empty debug hook and it will crash, Lua interface bug? – Anders Jul 27 '12 at 08:06
  • Have you tried running the user's code in a coroutine? You should be able to yield it from the debug hook. – Deco Jul 31 '12 at 05:38
  • Good idea, I guess you mena the lua.GetFunction right? It returns a instance of LuaFunction, and the only methods on that class is Call and Dispose, and dispose does nothing :/ – Anders Jul 31 '12 at 12:57

1 Answers1

2

How the ... do you abort a DoString in Lua C#!?

It doesn't matter if it's from C#, C, C++, or whatever. Lua doesn't simply stop just because you want it to. It is inherently single-threaded, which means that the execution of a Lua script will stop when it returns. And since it's not legal to call into a Lua state from one thread while one thread is active, there's not much you can do directly.

Lua simply isn't intended to be used in that way.

if I dispose the lua class

You can't dispose of the Lua state while it's running. If throwing an exception from a debug hook doesn't work, then there is simply nothing you can do.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thanks for the reply. How do you signal a Lua error? I've tried just do a lua.DoString("Rubbish") call in the hook but it wont help – Anders Jul 24 '12 at 08:08
  • @Anders: Actually, I realised that LuaInterface's error mechanism may not work, since Lua's errors work via setjmp/longjump, which likely does not play nice with LuaInterface. So odds are good you simply can't do this. – Nicol Bolas Jul 24 '12 at 08:13
  • I dont like to rely on the user doing things right. How could they design it this badly :/ – Anders Jul 24 '12 at 08:18
  • @Anders: What's bad about it? Lua is doing exactly what it's supposed to: executing the code it was given. As previously stated, it's a single-threaded API. Under single-threaded rules, it is impossible to cancel or otherwise close it from the outside world. Because there *is no* outside world in a single-threaded system. It's not bad; it's just not doing what *you* want ;) Oh, and you might be able to throw an exception as a means of firing an error. – Nicol Bolas Jul 24 '12 at 08:24
  • Well, this is a client app, and it will hang the entire app. Apps that hang are lame... I tried from the hook doing a DoString("line that throws error") but it wont interrupt the infinite DoString – Anders Jul 24 '12 at 08:31
  • I see that you are a game developer and probably assume this is for a game and that the scripts will be included in the source, thats not the case, this is a LUA IDE for a Programmable input emulator. So its lame that is hangs, I would through out Visual Studio if it hang on me just because I did a error in my code :D https://github.com/AndersMalmgren/FreePIE – Anders Jul 24 '12 at 08:36
  • Please see question edit – Anders Jul 24 '12 at 09:48
  • @Anders: "I tried from the hook doing a DoString("line that throws error")" Why didn't you try throwing an exception like I suggested? – Nicol Bolas Jul 24 '12 at 18:55
  • A C# exception inside the hook delegate? That will only crash the program – Anders Jul 25 '12 at 15:59
  • @Anders: Does it? LuaInterface's docs say that it will transform C# exceptions into Lua errors. Have you at least tried it? – Nicol Bolas Jul 25 '12 at 18:33
  • Most of the time it does nothing, and sometimes it crashes with a "Attempted to read or write protected memory"-exception. Thanks for all you're help I guess I'll have to accept that this is impossible in Lua, really strange they didnt think of supporting this.. – Anders Jul 26 '12 at 06:33
  • @Anders: Blame the LuaInterface people; the firing an error in the debug hook method works when you use Lua directly from C/C++. Though to be fair, it's very easy for a script to work around that too, just by calling `pcall` in the script. Ultimately, Lua is not intended to be used in an environment where you can't put reasonable trust in the script. – Nicol Bolas Jul 26 '12 at 06:57