-
Notifications
You must be signed in to change notification settings - Fork 190
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
Problem with multi thread access on GraalJS #59
Comments
Hi @Brunomachadob, thanks for your interest in Graal.js. The code in your example attempts to access the same JS Context from multiple threads, concurrently. Concurrent access to the same JS This is conceptually equivalent to the scenario in this test. Graal.js does allow multi-threading, but the application must ensure (e.g., using thread locals or synchronization to enter the right |
Thanks for your quick response @eleinadani. Yes, I understood the problem regarding the concurrence of the two threads that were active at the same time. What I'm struggling to understand is how I could avoid this problem, considering that:
Looking on your tests, you have complete control of the execution cycle, your created threads and the JS Context, which is not my case. Where I could create/enter/leave Contexts in the provided example? Thanks! |
Currently, creating, entering and leaving another JS context from JavaScript is not supported. As discussed in our documentation, you can however use a Java class (e.g., For example, you could define:
and create the servlet from your JS code in the following way:
Since it is not possible to share JS objects between contexts, the JS callback has to be passed to the |
Thanks for your insights @eleinadani This kind of change is going to demand a lot of rework, in a lot of libraries. Also, this will complicate things, as today we only develop in JavaScript, with this kind of change will need to start doing some Java and building jars to use as dependencies. Assuming that only our framework use this kind of usage of threads. Here we have another example scheduler that would need some work too. And another, but this isn't ours: httpsrv.js Do you guys have any plans to include some kind of option to disable this validation? I'm sure this is going to break a lot of thinks that was written for Nashorn. Let's say we simply remove that validation, my applications that today, is sharing contexts between threads using Nashorn will behave quite differently in GraalJS? edited GraalJS had some kind of change in JavaScript execution that made this validation needed or it was just a desired feature to include, to help minimize concurrency errors? Thanks! |
Yes, the main reason why we do not allow two concurrent threads to access the same |
Thanks @eleinadani. Similar question, but slightly different. Is it possible to share the same javascript functions, but execute with different variables through Bindings or Values? For example, I have 5 orders that need to be evaluated through the same source, but I don't want to reload and re-compile the script. I want to compile/prepare the sources, but execute them with different Order object. Like you said, I am running into thread race issue with Nashorn, and looking for alternative and ran into this thread. Thanks for your response in advance.. Cheers |
We recently published this article where we summarize of the main aspects of Graal.js' threading support. I am therefore closing this issue, but feel free to re-open if you have further questions. |
I have to disagree with your strict threading policy. Like in Nashorn, it should be the responsibility of the user how to handle multi-threaded access. Here is an example: We have a MQTT connection and subscribe on a topic as a consumer. This is done asynchronously with a listener that gets called when a message arrives. That's a quite common pattern in messaging. Everything is initiated from the JS side. In my script I register the listener and a callback when a message arrives:
The listener is called from the MQTT client thread. I have no access to it. I can't synchronize access to the context because I'm within the script. So this listener is called and I only use it to register another callback This works in Nashorn, of course. That it might turn into race conditions is true but to avoid that is the responsibility of the user, not of the JVM. |
@iitsoftware: I strongly agree with your statement that this should be the responsibility of the developer. Did you manage to find a solution for that problem? While it is possible to start new threads with Java-implemented Runnables, I am curious if you happened to find a "JavaScript-only" solution... |
Yes, I did:
https://medium.com/swlh/porting-from-nashorn-how-to-handle-multi-threading-in-graal-js-957e359b7df5
Am 01.08.2020 um 18:58 schrieb hjerabek <[email protected]>:
@iitsoftware: I strongly agree with your statement that this should be the responsibility of the developer. Did you manage to find a solution for that problem? While it is possible to start new threads with Java-implemented Runnables, I am curious if you happened to find a "JavaScript-only" solution...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
@iitsoftware: Thanks for the link and sharing your code and experiences. Using a custom Proxy class is a clever and elegant solution. Yet, IMO there is no equivalent, JavaScript-only solution of this approach. If I, for example, use |
Yes, you‘d at least need a custom Proxy class that instantiates the original Java interface and binds it to the Proxy. So this needs to be part of your runtime somehow.
Find the code here:
https://github.com/iitsoftware/graaljs-concurrency-problem
Am 01.08.2020 um 20:54 schrieb hjerabek <[email protected]>:
@iitsoftware: Thanks for the link and sharing your code and experiences. Using a custom Proxy class is a clever and elegant solution. Yet, IMO there is no equivalent, JavaScript-only solution of this approach. If I, for example, use Java.extend on java.lang.reflect.Proxy to create a custom Proxy class, it should result in the original IllegalStateException because the functions used to extend Proxy are bound to the context they are defined in. Would you agree that probably any workaround for this problem will require at least some component to be coded in Java?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
I wonder how can this being resolve using JSR223. |
I happened to notice this issue and am surprised by the solution based on AOP. In my humble opinion, thread-synchronizing belongs to the JS engine. Here's another solution without locks in Java for your reference, though I know it makes little sense to folks who are locked in GraalJS. At least, in my solution, the JS engine does that work transparently. |
Thanks @iitsoftware, will try that. All together comes to conclusion of incompatibility to JSR223. I tried the follow:
Still in same position with same exception. |
Hey! Thank you guys for such a great work on GraalJS, I'm really enjoying working with it.
I'm currently working in a development platform called ThrustJS (sorry about the docs that are only in Portuguese by now) in my company, it's written in JavaScript, powered by the JVM using Nashorn. We already have a lot of projects running in this platform.
Recently I started to work on this platform to run on both Nashorn and GraalJS, everything is going well so far, except by this error I'm getting when I try to use the Apache Tomcat 9 as an API server.
Today we don't have any sync/shared state problems as we hardly encourage everyone to use more functional/stateless approaches to build applications and also, we always create a new script context on every
require
on a file. (See creating a new context on require)I have isolated the problem in a simple Gist, the example works using
jjs
but doesn't work with 'js'.I did some digging in the GraalJS/GraalVM codebase and issues and it seems we don't have any kind of option to disable this.
Could you guys please give me some advice on this?
Thank's in advance.
The text was updated successfully, but these errors were encountered: