Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to detect infinite loop and stop execution? #324

Open
rohts-patil opened this issue Mar 6, 2023 · 2 comments
Open

How to detect infinite loop and stop execution? #324

rohts-patil opened this issue Mar 6, 2023 · 2 comments

Comments

@rohts-patil
Copy link

Let's say I have an MVEL expression that is causing an infinite loop. Is there any API in the MVEL library that allows me to set a timeout for executing the expression?

Sample expression:-
Serializable compiled = MVEL.compileExpression("while(true)\n" + "System.out.println(\"Hello\");");
Map<String, Object> vars = Collections.singletonMap("a", (Object) Collections.emptyMap());
MVEL.executeExpression(compiled, vars);

This will keep on executing.

@MalcolmOdd
Copy link
Contributor

I don't believe MVEL supports this directly but you could do something like this:

      ExecutorService executor = Executors.newSingleThreadExecutor();
      Serializable compiled = MVEL.compileExpression("while(true)\n" + "System.out.println(\"Hello\");");
      Map<String, Object> vars = Collections.singletonMap("a", (Object) Collections.emptyMap());
      Future<Object> futureResult = executor.submit(() -> MVEL.executeExpression(compiled, vars));
      try {
	  futureResult.get(5, TimeUnit.SECONDS);
      }
      catch(TimeoutException tex) {
	  System.out.println("Timeout detected");
      }

@rohts-patil
Copy link
Author

This won't work. The original thread won't be interrupted and the execution would go on, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants