-
Notifications
You must be signed in to change notification settings - Fork 43
Detach only external threads #940
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
Conversation
46dc795 to
be65f4e
Compare
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.
As annoying as it will be to do (given how often it's used), I'd prefer to see a slight change to how this is done:
Add a aws_jvm_thread_attachment_context struct (or similar name) that contains the env and the bool, ala:
struct aws_jvm_thread_attachment_context {
JNIEnv *env;
bool should_detach;
};
and have aws_jni_acquire_thread_env() return the struct by value. Then the pattern becomes:
struct aws_jvm_thread_attachment_context attachment_context = aws_jni_acquire_thread_env(jvm);
JNIEnv *env = attachment_context.env;
...
aws_jni_release(thread_env(jvm, &attachment_context);
I prefer encapsulating the attachment state in one type rather than splitting into two (and maybe more in the future?) separate fields.
Or maybe |
Issue #, if available:
The issue this PR fixes appears on Apple platforms with dispatch queue event loop enabled and using GraalVM.
Currently, when a native callback is executed, crt-java assumes it always runs on a dispatch queue thread. So, at the beginning of a callback, the thread attaches to JVM, and at the end - detaches from JVM. However, sometimes callbacks can be actually executed in the main thread, like in the bootstrap create-destroy test. This leads to callbacks attempting to detach the main JVM thread from JVM.
When executing in JVM mode, it seems such requests are simply being ignored, otherwise we should have seen errors. But in GraalVM this leads to the following error:
and then crash.
Description of changes:
This PR does for dispatch queue threads what is basically already done for CRT threads: detachment will happen only if attachment actually happened at the beginning of a callback.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.