Open vepo opened 1 year ago
Hi, Thank you for reporting this. Please take a look at this: Multithreading Let us know if that answers your question
Thanks @oubidar-Abderrahim, but this wiki page does not answer my question. My issue is that we create some classes in our Javascript code that are called by another framework (Kafka Streams). We could not find a way to synchronize these threads or create on context per thread.
Hi @iamstolis can you take a look into this please?
Is there a way to provide Class (or Context) factory to avoid using the same context in different threads?
No. You should not use the same (graal-js
) ScriptEngine
in multiple threads without a proper synchronization. I suggest you to either use one ScriptEngine
per thread or synchronize the access to one global ScriptEngine
.
We could not find a way to synchronize these threads ...
If I understand correctly then your problem is that some framework is using ScriptEngine
(that you provide) from multiple threads. You may try to provide your own implementation of ScriptEngine
that delegates to graal-js
ScriptEngine
. The methods of your wrapper could either synchronize the access or delegate to per-thread graal-js
ScriptEngine
s (that your wrapper ScriptEngine
will create/manage).
@iamstolis, I'm not sure if you understand correctly.
your problem is that some framework is using ScriptEngine (that you provide) from multiple threads.
No. We have a framework using ScriptEngine that does not create the threads, it provides some classes that will be used by other threads. The threads are created by another framework, in our case Kafka Streams.
In my sample, I have the onCustom
function that receives a stream and creates some classes on this stream. The function provided on filter
will be used in many classes. I can configure my Kafka Stream to use just one thread, but this is not a good scenario for scaling.
Description
I'm replacing Nashorn with graaljs in a Kafka Stream framework as a hookpoint engine. It's working fine for some of our extension points, but we have an extension point to configure some Kafka Stream steps.
This is the code we are using to test our implementation:
Our issue happens because after executing the execution Kafka Stream creates some threads and tries to call the created objects inside the javascript code. Then we have the exception bellow because more than one thread is accessing the same context simultaneously.
This is the way we instantiate our script engine:
Is there a way to provide Class (or Context) factory to avoid using the same context in different threads?
Environment
GraalVM JS version: 21.3.6 Java version: OpenJDK 8