Skip to content
Advertisement

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 –

JavaScript

This has been failing for me with the error –

JavaScript

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

Advertisement

Answer

You may want to use a more generic solution, that could potentially work with other scripting engines (e.g. rhino or nashorn), regardless of the built-in features:

JavaScript

Other advantage of this solution is that it allows you to use a thread-pool, hence giving you more control over how concurrent scripts are executed (e.g. you can limit the number of scripts being executed at the same time to one, or to the number of available CPU-cores, etc.).

Please note though, that the time limit specified in the example is the time that elapsed since the submission of the task, not the time that was actually spent on the execution of the given script.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement