2

我在 graalVM 文档中找到了沙盒选项作为设置 sandbox.MaxCPUTime 的一种方式,以限制线程运行的时间 - https://www.graalvm.org/reference-manual/embed-languages/

我试过以下代码 -

                try (Context context = Context.newBuilder("js")
                        .allowExperimentalOptions(true)
                        .option("sandbox.MaxCPUTime", "10s")
                        .option("sandbox.MaxCPUTimeCheckInterval", "5ms")
                        .build())
                {
                    try {
                        context.eval("js", "while(true);");
                        assert false;
                    } catch (PolyglotException e) {
                        // Triggered after 500ms;
                        // Context is closed and can no longer be used
                        // Error message: Maximum CPU time limit of 500ms exceeded.
                        assert e.isCancelled();
                        assert e.isResourceExhausted();
                    }
                    context.close(true);
                }

由于错误,这对我来说一直失败 -

java.lang.IllegalArgumentException: Could not find option with name sandbox.MaxCPUTime.

有没有更好的方法来实现这一点,或者我可以让这些沙箱选项起作用?

4

1 回答 1

3

您可能想要使用更通用的解决方案,它可能与其他脚本引擎(例如rhinonashorn)一起使用,而不管内置功能如何:

final ExecutorService executor = Executors.newSingleThreadExecutor();

final Context context = Context.newBuilder("js").build();
final Future<Object> futureResult = executor.submit(() -> context.eval("js", "while(true);"));

try {
    final Object result = futureResult.get(10, TimeUnit.SECONDS);
    System.out.println("Script evaluated within 10 seconds, result: " + result);
} catch (TimeoutException e) {
    context.interrupt(Duration.ZERO);
    System.out.println("Script not evaluated within 10 seconds, interrupted.");
}

System.out.println("Done.");

此解决方案的其他优点是它允许您使用线程池,从而使您可以更好地控制并发脚本的执行方式(例如,您可以将同时执行的脚本数量限制为一个,或限制为可用的 CPU 内核等)。

但请注意,示例中指定的时间限制是自提交任务以来经过的时间,而不是实际执行给定脚本所花费的时间。

于 2021-03-29T14:13:17.727 回答