GeezerVison is showing github.com — Avoid JVM crash by deleting JNI refs after calling GetMethodDeclaringClass by krk · Pull Request #981 · async-profiler/async-profiler
Conversation
This is a partial fix for #974, complementing JDK-8268364.
How has this been tested?
Backported JDK-8268364 into Java 17 and tested with reproducer at gist:
javac Main.java
gcc -shared -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -fPIC repro.cpp -orepro.so
# Low Xmx to pressure GC into unloading classes sooner.
java -agentpath:"$(pwd)/repro.so" -Xmx100m Main
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
}
|
|
|
|
|
|
jvmtiEnv* jvmti = VM::jvmti();
|
|
|
JNIEnv* jni = VM::jni();
|
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.
VM::jni() is not an expensive call, but it's not free either - not a trivial getter like VM::jvmti().
So, I'd create JNIEnv* once in Lookup constructor and cache it as a field.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
krk reacted with thumbs up emoji
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
char* method_sig = NULL;
|
|
|
|
|
|
jvmtiEnv* jvmti = VM::jvmti();
|
|
|
JNIEnv* jni = VM::jni();
|
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.
Similar to above: obtain JNIEnv* once in FrameName constructor.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
krk reacted with thumbs up emoji
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
_thread_names_lock(thread_names_lock),
|
|
|
_thread_names(thread_names)
|
|
|
_thread_names(thread_names),
|
|
|
_jni(VM::jni())
|
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.
There will be the same problem with native apps as above.
I suggest simply returning NULL from VM::jni() if _vm == NULL.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
krk reacted with thumbs up emoji
|
|
}
|
|
|
|
|
|
if (method_class) {
|
|
|
_jni->DeleteLocalRef(method_class);
|
D-D-H
Sep 3, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
Hi @krk ,
After investigating why the call to DeleteLocalRef is added here(cannot completely fix the problem), I think we can fix the problem in OpenJDK upstream.
Do you plan to fix it? If not, we will file an issue at OpenJDK.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
D-D-H
Sep 4, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
Do you propose a different fix?
If I understand correctly, the call to 'DeleteLocalRef' here is to reduce the possibility of a crash. That crash is actually a bug in JDK and not fixed by JDK-8268364. So we should file a new issue in OpenJDK to fix it.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
@D-D-H You are right: DeleteLocalRef is a cheap way to significantly decrease possibility of a crash, but fixing the root cause in OpenJDK is still highly desirable. I think it makes sense to integrate this change anyway, before the proper fix is released.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
krk, D-D-H, and yanglong1010 reacted with thumbs up emoji
D-D-H
Sep 9, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
apangin and yanglong1010 reacted with thumbs up emoji
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I ran the reproducer. The stack trace in hs_err log produced by this reproducer is different from the stack trace I provided in #974. The reproducer crashes in GC threads, D-D-H thinks this a JDK bug, DeleteLocalRef can alleviate this problem, but it should also be fixed on JDK (including JDK 18 and above).
The crash in #974 on JDK 8/11/17 is due to lack of JDK-8268364.
These are two different issues and need to be treated differently.
Am i right ?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Yes, there are two crashes, but they are both parts of the same issue.
JDK-8268364 only mitigates one possible race, but does not solve the problem completely.
|
krk and yanglong1010 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This is a partial fix, complementing JDK-8268364.
apangin
changed the title
Fix JVM crash by deleting refs after calling GetMethodDeclaringClass.
Avoid JVM crash by deleting JNI refs after calling GetMethodDeclaringClass
Sep 4, 2024
Reviewers
apangin
apangin left review comments
+1 more reviewer
D-D-H
D-D-H left review comments
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|