-
Notifications
You must be signed in to change notification settings - Fork 83
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
Nashorn sandbox does not catch Error
when using an ExecutorService
#168
Comments
Thank you for raising this issue as well! I think I understand what the issue is but I in order to be sure can you have a look at this test case: How can this best be modified to replicate the sought after behaviour? I think we would need to add a call into Java where a Anything that catches your eye in these places where some execution occurs. Does this look like the right direction? Once I can confirm the test case, can look further where best to patch the code. |
Correct, you can't catch So the fix requires:
|
Thank you for the further information! I extended the test case now. Currently it looks like if we throw an Before I make any changes, could you confirm:
public class TestIssue168_Error_Returns_Null {
public static class IFail {
public void doIt() {
throw new Error("I tried my best but I failed");
}
}
@Test(expected = java.lang.Error.class)
public void test() throws ScriptCPUAbuseException, ScriptException, NoSuchMethodException {
NashornSandbox sandbox = NashornSandboxes.create();
try {
sandbox.setExecutor(Executors.newSingleThreadExecutor());
sandbox.inject("iFail", new IFail());
String code = "iFail.doIt();";
sandbox.eval(code);
} finally {
sandbox.getExecutor().shutdown();
}
}
} |
That's certainly the most backwards-compatible solution (and having
I don't understand -- you want (Or maybe I misunderstood your question...) |
The Nashorn sandbox does not catch
Error
instances, e.g.ExceptionInInitializerError
, when running with anExecutorService
.This means that if you run JS code that calls back to Java methods, and one of the calls throws an
Error
, thesandbox.eval
call will simply returnnull
rather than throwingScriptException
, and theError
will be thrown as an uncaught exception in theExecutorService
thread. (If you're not using anExecutorService
, then theError
is simply thrown to the caller of `sandbox.eval as expected.)Presumably the fix is to catch
Error
, notException
, insandbox.eval
, and then wrap theError
object in aScriptException
.The text was updated successfully, but these errors were encountered: