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

Parallel ScriptEngine script compilation #801

Open
neon-dev opened this issue Feb 21, 2024 · 0 comments
Open

Parallel ScriptEngine script compilation #801

neon-dev opened this issue Feb 21, 2024 · 0 comments

Comments

@neon-dev
Copy link

neon-dev commented Feb 21, 2024

Using the ScriptEngine API to compile scripts fails when called from different threads.
This is because the GraalJSBindings of the engine context are accessed during compilation, leading to at least three different types of errors:

  1.  Exception in thread "main" java.lang.NullPointerException
     	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
     	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
     	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
     	at java.base/java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:542)
     	at java.base/java.util.concurrent.ForkJoinTask.reportException(ForkJoinTask.java:567)
     	at java.base/java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:670)
     	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateParallel(ForEachOps.java:160)
     	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateParallel(ForEachOps.java:174)
     	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
     	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
     	at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:765)
     	at Test.main(Test.java:25)
     Caused by: java.lang.NullPointerException: Cannot invoke "java.util.Map.entrySet()" because "this.global" is null
     	at com.oracle.truffle.js.scriptengine.GraalJSBindings.entrySet(GraalJSBindings.java:198)
     	at java.base/java.util.AbstractMap.containsKey(AbstractMap.java:148)
     	at java.scripting/javax.script.SimpleScriptContext.getAttribute(SimpleScriptContext.java:158)
     	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.createSource(GraalJSScriptEngine.java:462)
     	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.compile(GraalJSScriptEngine.java:653)
     	...
    
  2.  java.lang.IllegalStateException: Multi threaded access requested by thread Thread[#32,ForkJoinPool.commonPool-worker-10,5,main] but is not allowed for language(s) js.
     	at com.oracle.truffle.polyglot.PolyglotEngineException.illegalState(PolyglotEngineException.java:133)
     	at com.oracle.truffle.polyglot.PolyglotContextImpl.throwDeniedThreadAccess(PolyglotContextImpl.java:1392)
     	at com.oracle.truffle.polyglot.PolyglotContextImpl.checkAllThreadAccesses(PolyglotContextImpl.java:1058)
     	at com.oracle.truffle.polyglot.PolyglotContextImpl.enterThreadChanged(PolyglotContextImpl.java:884)
     	at com.oracle.truffle.polyglot.PolyglotEngineImpl.enterCached(PolyglotEngineImpl.java:2080)
     	at com.oracle.truffle.polyglot.HostToGuestRootNode.execute(HostToGuestRootNode.java:109)
     	at com.oracle.truffle.api.impl.DefaultCallTarget.callDirectOrIndirect(DefaultCallTarget.java:85)
     	at com.oracle.truffle.api.impl.DefaultCallTarget.call(DefaultCallTarget.java:102)
     	at com.oracle.truffle.polyglot.PolyglotMap.entrySet(PolyglotMap.java:124)
     	at com.oracle.truffle.js.scriptengine.GraalJSBindings.entrySet(GraalJSBindings.java:198)
     	at java.base/java.util.AbstractMap.containsKey(AbstractMap.java:148)
     	at java.scripting/javax.script.SimpleScriptContext.getAttribute(SimpleScriptContext.java:158)
     	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.createSource(GraalJSScriptEngine.java:462)
     	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.compile(GraalJSScriptEngine.java:653)
     	...
     	Suppressed: Attached Guest Language Frames (1)
    
  3.  java.lang.IllegalStateException: Multi threaded access requested by thread Thread[#44,ForkJoinPool.commonPool-worker-11,5,main] but is not allowed for language(s) js.
     	at com.oracle.truffle.polyglot.PolyglotEngineException.illegalState(PolyglotEngineException.java:133)
     	at com.oracle.truffle.polyglot.PolyglotContextImpl.throwDeniedThreadAccess(PolyglotContextImpl.java:1392)
     	at com.oracle.truffle.polyglot.PolyglotContextImpl.checkAllThreadAccesses(PolyglotContextImpl.java:1058)
     	at com.oracle.truffle.polyglot.PolyglotContextImpl.enterThreadChanged(PolyglotContextImpl.java:884)
     	at com.oracle.truffle.polyglot.PolyglotEngineImpl.enterCached(PolyglotEngineImpl.java:2080)
     	at com.oracle.truffle.polyglot.PolyglotEngineImpl.enterIfNeeded(PolyglotEngineImpl.java:2008)
     	at com.oracle.truffle.polyglot.PolyglotValueDispatch.hostEnter(PolyglotValueDispatch.java:1256)
     	at com.oracle.truffle.polyglot.PolyglotContextImpl.parse(PolyglotContextImpl.java:1649)
     	at com.oracle.truffle.polyglot.PolyglotContextDispatch.parse(PolyglotContextDispatch.java:65)
     	at org.graalvm.polyglot.Context.parse(Context.java:483)
     	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.checkSyntax(GraalJSScriptEngine.java:680)
     	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.compile(GraalJSScriptEngine.java:664)
     	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.compile(GraalJSScriptEngine.java:654)
     	...
    

How to reproduce

Here is a simple reproducer: reproducer.zip
Commented out lines work around these issues (explained below).

Possible workaround

  1. Synchronize on the engine bindings to fix all compliation errors:
    synchronized (scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE)) {
      ((Compilable) scriptEngine).compile(jsScript);
    }
  2. Add this line right after instantiating the script engine:
    scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).entrySet();
    It resolves compilation slowdowns by initializing the GraalJSBindings context early.
    Synchonization from 1) somehow does not take care of this internal race condition. Maybe it's a thread visibility issue, not sure.

Another workaround would be to just replace the engine bindings with SimpleBindings, but this massively degrades script compilation performance, because the engine will then create new GraalJSBindings + context per compilation.

Solution?

Synchronize on the context here

and here

Maybe additional synchronization here would fix performance issues from 2):
private void requireContext() {
if (context == null) {
initContext();
}
}

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

1 participant