How can I discard the LuaJ coroutine LuaThread?

I am experimenting with a game mechanic in which players can run scripts on gaming computers. Script execution will be limited by the resource at the level of the game process to a certain number of instructions per mark.

The following proof of concept demonstrates the basic level of sandboxing and throttling of arbitrary user code. It successfully launches ~ 250 instructions of a poorly created "user input", and then discards the coroutine. Unfortunately, the Java process never ends. A little research shows that LuaThread , created by LuaJ for coroutines, hangs forever.

SandboxTest.java:

 public static void main(String[] args) { Globals globals = JsePlatform.debugGlobals(); LuaValue chunk = globals.loadfile("res/test.lua"); chunk.call(); } 

Res / test.lua:

 function sandbox(fn) -- read script and set the environment f = loadfile(fn, "t") debug.setupvalue(f, 1, {print = print}) -- create a coroutine and have it yield every 50 instructions local co = coroutine.create(f) debug.sethook(co, coroutine.yield, "", 50) -- demonstrate stepped execution, 5 'ticks' for i = 1, 5 do print("tick") coroutine.resume(co) end end sandbox("res/badfile.lua") 

Res / badfile.lua:

 while 1 do print("", "badfile") end 

The docs suggest that a coroutine that is considered irreproducible will be garbage collected and an OrphanedThread exception will be thrown, signaling LuaThread to the end - but this never happens. My question has two parts:

  • Am I doing something fundamentally wrong to cause this behavior?
  • If not, how should I deal with this situation? From the source, it seems that if I can get a link to LuaThread in Java, I can forcibly abandon it by issuing interrupt() . Is that a good idea?

Link: Lua / Java / LuaJ - processing or interrupting endless loops and threads

EDIT: I submitted a bug report to LuaJ SourceForge. It discusses the main problem (threads are not garbage collected, as in the Lua specification), and offers some ways to get around it.

+4
source share
1 answer

This seems to be a limitation of LuaJ. I applied for a ticket earlier this year at Sourceforge, as I see you did too. The LuaThread class LuaThread not store references to the Java threads it creates, so you cannot interrupt() those threads without modifying the LuaJ core to expose them:

new Thread(this, "Coroutine-"+(++coroutine_count)).start();

It might be dangerous to interrupt these threads without adding the appropriate cleanup code to LuaJ.


The documentation you provided for OrphanedThread also tells us that the scope is defining:

"An error has been detected that indicates that the lua thread that is no longer referenced has been detected. The java thread to which it was selected must match LuaThread, which is used as a coroutine that cannot be resumed again because it is no longer refer to the LuaThread with which it is associated.Instead of blocking resources permanently, this error occurs and should completely go to the thread of the Thread.run () method.

Your sample code does not cause all LuaThread links to disappear, so you should not expect an exception to be thrown. CoroutineLib documentation indicates: Coroutines that are yielded but never resumed to complete their execution may not be collected by the garbage collector , so OutOfMemoryError should be expected from the code that you specified in SourceForge if I am not mistaken. LuaThread:52 also indicates: Applications should not catch OrphanedThread, because it can break the thread safety of luaj. , which is another obstacle.


There are also differences between the empty and non-empty while contours in Lua / J. IIRC, empty loops ( while true do end ) do not obey all tuple / ticket rules. * Since no action occurs in an empty loop, there is no possibility for certain interceptions (I need to check this again, so please correct me otherwise!).

The deployed version of LuaJ with the functionality we are looking for is used in the ComputerCraft module for Minecraft, although it is intended for mods only and is not open source.

+3
source

Source: https://habr.com/ru/post/1489984/


All Articles