Lua/Java/LuaJ-处理或中断无限循环和线程 [英] Lua / Java / LuaJ - Handling or Interrupting Infinite Loops and Threads

查看:347
本文介绍了Lua/Java/LuaJ-处理或中断无限循环和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LuaJ在Java中运行用户创建的Lua脚本.但是,运行永远不返回的Lua脚本会导致Java线程冻结.这也使线程不间断.我用以下命令运行Lua脚本:

I'm using LuaJ to run user-created Lua scripts in Java. However, running a Lua script that never returns causes the Java thread to freeze. This also renders the thread uninterruptible. I run the Lua script with:

JsePlatform.standardGlobals().loadFile("badscript.lua").call();

badscript.lua包含while true do end.

我希望能够自动终止停留在不屈不挠的循环中的脚本,并且还允许用户在运行时手动终止其Lua脚本.我已经读过 debug.sethook

I'd like to be able to automatically terminate scripts which are stuck in unyielding loops and also allow users to manually terminate their Lua scripts while they are running. I've read about debug.sethook and pcall, though I'm not sure how I'd properly use them for my purposes. I've also heard that sandboxing is a better alternative, though that's a bit out of my reach.

此问题也可能仅扩展到Java线程.我还没有找到有关中断卡在while (true);中的Java线程的任何权威信息.

This question might also be extended to Java threads alone. I've not found any definitive information on interrupting Java threads stuck in a while (true);.

在线 Lua演示非常有前景,但是似乎不良"脚本的检测和终止是在CGI脚本中完成的,而不是在Lua中完成的.我能够使用Java调用CGI脚本,然后又调用Lua脚本吗?不过,我不确定是否允许用户手动终止其脚本.我丢失了Lua演示源代码的链接,但我手边有它.这是魔术路线:

The online Lua demo was very promising, but it seems the detection and termination of "bad" scripts is done in the CGI script and not Lua. Would I be able to use Java to call a CGI script which in turn calls the Lua script? I'm not sure that would allow users to manually terminate their scripts, though. I lost the link for the Lua demo source code but I have it on hand. This is the magic line:

tee -a $LOG | (ulimit -t 1 ; $LUA demo.lua 2>&1 | head -c 8k)

有人可以指出我正确的方向吗?

Can someone point me in the right direction?

某些来源:

  • Embedded Lua - timing out rogue scripts (e.g. infinite loop) - an example anyone?
  • Prevent Lua infinite loop
  • Embedded Lua - timing out rogue scripts (e.g. infinite loop) - an example anyone?
  • How to interrupt the Thread when it is inside some loop doing long task?
  • Killing thread after some specified time limit in Java

推荐答案

我在同一个问题上苦苦挣扎,在仔细研究了调试库的实现之后,我创建了一个类似于David Lewis提出的解决方案,但是通过提供我自己的DebugLibrary:

I struggled with the same issue and after some digging through the debug library's implementation, I created a solution similar to the one proposed by David Lewis, but did so by providing my own DebugLibrary:

package org.luaj.vm2.lib;

import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;

public class CustomDebugLib extends DebugLib {
    public boolean interrupted = false;

    @Override
    public void onInstruction(int pc, Varargs v, int top) {
        if (interrupted) {
            throw new ScriptInterruptException();
        }
        super.onInstruction(pc, v, top);
    }

    public static class ScriptInterruptException extends RuntimeException {}
}

只需从新线程内部执行脚本,然后将interrupted设置为true即可停止执行.引发该异常时,将封装该异常作为LuaError的原因.

Just execute your script from inside a new thread and set interrupted to true to stop the execution. The exception will be encapsulated as the cause of a LuaError when thrown.

这篇关于Lua/Java/LuaJ-处理或中断无限循环和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆