Java: Improve robustness against JVM crashes on exit.

The JVM doesn't always do a good job of telling JNI modules that the JVM
is going away, which results in a crash in the JavaGlobal and/or
JavaWeakGlobal destructors as they try to delete the associated references
after the JVM has already gone away.

To protect against this, the Notifier now has a static variable that's set
when the Notifier instance (a singleton) is destroyed.  This is used by
JavaGlobal and JavaWeakGlobal to detect when a process exit is in process.
This commit is contained in:
Peter Johnson
2015-09-14 22:00:22 -07:00
parent 6d8e796932
commit 51eb96903c
8 changed files with 19 additions and 3 deletions

View File

@@ -108,7 +108,7 @@ class JavaGlobal {
JavaGlobal(JNIEnv *env, T obj)
: m_obj(static_cast<T>(env->NewGlobalRef(obj))) {}
~JavaGlobal() {
if (!jvm) return;
if (!jvm || nt::NotifierDestroyed()) return;
JNIEnv *env;
if (jvm->AttachCurrentThread(reinterpret_cast<void **>(&env), nullptr) !=
JNI_OK)
@@ -133,7 +133,7 @@ class JavaWeakGlobal {
JavaWeakGlobal(JNIEnv *env, T obj)
: m_obj(static_cast<T>(env->NewWeakGlobalRef(obj))) {}
~JavaWeakGlobal() {
if (!jvm) return;
if (!jvm || nt::NotifierDestroyed()) return;
JNIEnv *env;
if (jvm->AttachCurrentThread(reinterpret_cast<void **>(&env), nullptr) !=
JNI_OK)