GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@krk

@krk krk commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

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.

All reactions
apangin
apangin reviewed Sep 2, 2024
View reviewed changes
Comment thread src/flightRecorder.cpp Outdated
}

jvmtiEnv* jvmti = VM::jvmti();
JNIEnv* jni = VM::jni();

@apangin apangin Sep 2, 2024

Copy link
Copy Markdown
Collaborator

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
All reactions
  • 👍 1 reaction

@krk krk Sep 2, 2024

Copy link
Copy Markdown
Contributor Author

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.

Thanks, cached JNIEnv.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/frameName.cpp Outdated
char* method_sig = NULL;

jvmtiEnv* jvmti = VM::jvmti();
JNIEnv* jni = VM::jni();

@apangin apangin Sep 2, 2024

Copy link
Copy Markdown
Collaborator

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
All reactions
  • 👍 1 reaction

@krk krk Sep 2, 2024

Copy link
Copy Markdown
Contributor Author

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.

Cached as above.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@krk
krk force-pushed the fjmi-974 branch 2 times, most recently from fe2f917 to 340786f Compare September 2, 2024 17:12
apangin
apangin reviewed Sep 2, 2024
View reviewed changes
Comment thread src/frameName.cpp
_thread_names_lock(thread_names_lock),
_thread_names(thread_names)
_thread_names(thread_names),
_jni(VM::jni())

@apangin apangin Sep 2, 2024

Copy link
Copy Markdown
Collaborator

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
All reactions
  • 👍 1 reaction
D-D-H
D-D-H reviewed Sep 3, 2024
View reviewed changes
Comment thread src/flightRecorder.cpp
}

if (method_class) {
_jni->DeleteLocalRef(method_class);

@D-D-H D-D-H Sep 3, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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.

All reactions

@krk krk Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

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.

I have created a backport into 17 for JDK-8268364 at openjdk/jdk17u-dev#2839. Do you propose a different fix?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@D-D-H D-D-H Sep 4, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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.

All reactions

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

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
All reactions
  • 👍 3 reactions

@D-D-H D-D-H Sep 9, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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.

My colleague Liang created an issue for this problem:
Issue: https://bugs.openjdk.org/browse/JDK-8339725
PR: https://git.openjdk.org/jdk/pull/20907

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

apangin and yanglong1010 reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

@apangin apangin Sep 9, 2024

Copy link
Copy Markdown
Collaborator

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.

Cool, thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yanglong1010

yanglong1010 commented Sep 3, 2024

Copy link
Copy Markdown
Contributor

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.

  • Avoid JVM crash by deleting JNI refs after calling GetMethodDeclaringClass #981 Need to fix all JDKs . Use DeleteLocalRef in async-profiler to alleviate.
  • Lookup::fillJavaMethodInfo may cause crash on JDK 8/11/17 #974 Need to backport JDK-8268364. Add workaround in async-profiler for released JDKs.

Am i right ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apangin

apangin commented Sep 4, 2024

Copy link
Copy Markdown
Collaborator

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
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@krk
Fix JVM crash by deleting refs after calling GetMethodDeclaringClass.
49990b9
This is a partial fix, complementing JDK-8268364.
@krk
krk force-pushed the fjmi-974 branch from 340786f to 49990b9 Compare September 4, 2024 10:13
@apangin 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
@apangin
apangin merged commit b9a3737 into async-profiler:master Sep 4, 2024
@jbachorik jbachorik mentioned this pull request Sep 6, 2024
Deallocate jclass reference promptly DataDog/java-profiler#127
Closed
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@apangin apangin apangin left review comments
+1 more reviewer
@D-D-H 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

Labels

None yet

Projects

None yet

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.

4 participants

@krk @yanglong1010 @apangin @D-D-H
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.