Wednesday 31 March 2021

How to exit infinite JS execution loop when reading/loading Javascript in Java using GraalVM?

I found sandbox options as a way to set sandbox.MaxCPUTime in the graalVM documentation, to limit how long the thread runs - https://www.graalvm.org/reference-manual/embed-languages/

I've tried the following code -

                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);
                }

This has been failing for me with the error -

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

Is there a better way to achieve this or a way I can make these sandbox options work?



from How to exit infinite JS execution loop when reading/loading Javascript in Java using GraalVM?

No comments:

Post a Comment