-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow control of throwDeniedThreadAccess
via TruffleContext.threadAccessDeniedHandler
#8266
Allow control of throwDeniedThreadAccess
via TruffleContext.threadAccessDeniedHandler
#8266
Conversation
With
The good thing from the Truffle API perspective is:
The |
I like the idea in principle. What I like about this approach:
What I don't like about this approach:
to something like this (just a sketch)
|
OK, I'll write some unit tests and then we can continue the review. I am not sure I fully understand all the comments, for example:
Sure, I only care about Graal.js - all the other languages we are interested in are mutli threading ready. All I need is to make sure when there is a collision, it doesn't end up that fatal as right now and there is a way to recover. I don't get the comment about multi-threaded languages. We can sort it out when I prepare some unit tests for review. But I'd prefer to keep my PR as minimal as possible - solving complete Truffle corner cases is job for a professional, not for an outsider like me.
Do you mean those I'll find some time this week and add some test cases to this PR. |
The first test added in addb748 verifies that all the ways that originally yielded The other test (added in f91faeb) verifies that the @chumer, what additional tests you want me to provide? |
Hello @chumer, is there anything else I shall do to get this API change in? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A great step forward. Some of the things I previously commented on were unfortunately not adressed. Added a few more comments.
truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java
Show resolved
Hide resolved
...cle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/MultiThreadedLanguageTest.java
Outdated
Show resolved
Hide resolved
...cle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/MultiThreadedLanguageTest.java
Outdated
Show resolved
Hide resolved
...fle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextImpl.java
Outdated
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
...m.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotThreadAccessException.java
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
19f1f07
to
f1373f0
Compare
afe103d
to
0172e68
Compare
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java
Outdated
Show resolved
Hide resolved
...cle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/MultiThreadedLanguageTest.java
Outdated
Show resolved
Hide resolved
...cle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/MultiThreadedLanguageTest.java
Outdated
Show resolved
Hide resolved
...cle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/MultiThreadedLanguageTest.java
Outdated
Show resolved
Hide resolved
...m.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotThreadAccessException.java
Show resolved
Hide resolved
...m.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotThreadAccessException.java
Outdated
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
I am trying to compile Enso with GraalVM 24.2.0 snapshot to verify that oracle/graal#8266 changes would work for Enso. Among the problems I am facing is a dependency on `org.graalvm.collections` because of using `Pair`. I don't think we need such dependency. Let's avoid it by using our own `record` with two components.
I tried to use this change in Enso. I am struggling. Enso still uses diff --git engine/runtime-fat-jar/src/main/java/module-info.java engine/runtime-fat-jar/src/main/java/module-info.java
index 87816c9723..9d577e04bb 100644
--- engine/runtime-fat-jar/src/main/java/module-info.java
+++ engine/runtime-fat-jar/src/main/java/module-info.java
@@ -9,6 +9,7 @@ open module org.enso.runtime {
// works.
requires org.enso.profiling;
requires org.enso.ydoc;
+ requires org.graalvm.collections;
requires org.graalvm.polyglot;
requires org.graalvm.truffle;
requires static org.slf4j;
diff --git engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/ForeignMethodInvokeTest.java engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/ForeignMethodInvokeTest.java
index 3cb3722f50..6a2822161e 100644
--- engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/ForeignMethodInvokeTest.java
+++ engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/ForeignMethodInvokeTest.java
@@ -13,7 +13,6 @@ import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.junit.AfterClass;
import org.junit.BeforeClass;
-import org.junit.Ignore;
import org.junit.Test;
public class ForeignMethodInvokeTest {
@@ -91,7 +90,6 @@ public class ForeignMethodInvokeTest {
assertEquals(12, res2.getArrayElement(2).asInt());
}
- @Ignore
@Test
public void testParallelInteropWithJavaScript() throws Exception {
var source =
diff --git engine/runtime-language-epb/src/main/java/org/enso/interpreter/epb/EpbContext.java engine/runtime-language-epb/src/main/java/org/enso/interpreter/epb/EpbContext.java
index 0eed392545..929818ed01 100644
--- engine/runtime-language-epb/src/main/java/org/enso/interpreter/epb/EpbContext.java
+++ engine/runtime-language-epb/src/main/java/org/enso/interpreter/epb/EpbContext.java
@@ -44,6 +44,7 @@ final class EpbContext {
if (innerContext == null) {
innerContext =
env.newInnerContextBuilder()
+ .onDeniedThreadAccess(this::deniedThreadAccess)
.initializeCreatorContext(true)
.inheritAllAccess(true)
.config(INNER_OPTION, "yes")
@@ -52,6 +53,14 @@ final class EpbContext {
}
}
+ private void deniedThreadAccess(Throwable t) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException ex) {
+ // go on
+ }
+ }
+
/**
* @param node the location of context access. Pass {@code null} if not in a node.
* @return the proper context instance for the current {@link I am going to remove the API changes in |
@chumer, I have addressed all your API change requests. Please review again. Right now I am struggling with endless loop in
It is a result of a bad merge. I'll address it soon. However I don't expect it to impact the observable API - e.g. you can review even now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor changes requested
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java
Outdated
Show resolved
Hide resolved
truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleContext.java
Outdated
Show resolved
Hide resolved
truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleContext.java
Show resolved
Hide resolved
...e/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextConfig.java
Outdated
Show resolved
Hide resolved
...e/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextConfig.java
Outdated
Show resolved
Hide resolved
...e/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextConfig.java
Outdated
Show resolved
Hide resolved
Change log updated in 7149f99 |
7149f99
to
85854c0
Compare
Squashed into a single commit in 85854c0 |
throwDeniedThreadAccess
throwDeniedThreadAccess
via TruffleContext.threadAccessDeniedHandler
...src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotLanguageContext.java
Outdated
Show resolved
Hide resolved
sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/Context.java
Outdated
Show resolved
Hide resolved
...m.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotThreadAccessException.java
Outdated
Show resolved
Hide resolved
....oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/MultiThreadedLanguage.java
Outdated
Show resolved
Hide resolved
...com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/ContextSharingTest.java
Outdated
Show resolved
Hide resolved
Jaroslav, I am about to merge your work. In case you want your name on it, please resolve conflict, apply the following diff:
and then squash into one commit. If you don't care about your name being on this, I can do all that for you. Please let me know soon, thanks. |
It is not enough to apply the patch. Conflict still remains. I had to add a810ee8 - hopefully it is the last time I need to manually merge conflicts in Finally squashed in b7f3b4c |
a810ee8
to
b7f3b4c
Compare
Thanks, the patch was not intended to resolve the conflict, these we just further changes that needed to be applied after the conflict was resolved. |
I see the threadAccessDeniedHandler in the official Javadoc. Thank you for your help accepting my patch! |
Let's fix #8033 by controlling what happens when
throwDeniedThreadAccess
is called.The simplest way to address #8033 is to replace the current action in
throwDeniedThreadAccess
- e.g. throwPolyglotEngineException.illegalState
with a callback to embedder to decide what to do - don't throw, but retry. However calling foreign code at the moment ofthrowDeniedThreadAccess
is slightly dangerous as internal lock onPolyglotContextImpl
is being held. As such, let's start with the first step 82b14c3 - let's make sure the exception is always thrown without holding internal lock(s). To do so, I am introducing an internal checked exception and propagating it up to the point where the lock isn't held. Only then it is converted to originalPolyglotEngineException.illegalState
exception and thrown. The fact that the exception is checked, instructs thejavac
to check all paths that may yield thePolyglotThreadAccessException
and help me to never forget to handle the conversion. The unusualloop: for (;;) {... continue loop.... break loop}
style is used as a preparation for the next step...The next step then defines
Builder.onDeniedThreadAccess(Consumer<RuntimeException> handler)
which allows the embedders to decide whether to throw the exception or do something more intrinsic.The third step is to use the API. Great, it seems to work as planned.