--- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/jni_helper.c 2021-05-06 15:53:56.037724986 +0200 +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/jni_helper.c 2021-05-06 16:00:45.728548653 +0200 @@ -650,9 +650,11 @@ * every thread. You must be holding the jvmMutex when you call this * function. * + * @param[out] threadCreatedFromJava current thread origin (Java or native) + * * @return The JNIEnv on success; error code otherwise */ -static JNIEnv* getGlobalJNIEnv(void) +static JNIEnv* getGlobalJNIEnv(bool* threadCreatedFromJava) { JavaVM* vmBuf[VM_BUF_LENGTH]; JNIEnv *env; @@ -678,6 +680,7 @@ } if (noVMs == 0) { + *threadCreatedFromJava = false; //Get the environment variables for initializing the JVM hadoopClassPath = getClassPath(); if (hadoopClassPath == NULL) { @@ -757,10 +760,22 @@ } else { //Attach this thread to the VM vm = vmBuf[0]; - rv = (*vm)->AttachCurrentThread(vm, (void*)&env, 0); - if (rv != 0) { - fprintf(stderr, "Call to AttachCurrentThread " - "failed with error: %d\n", rv); + jint isAttached = (*vm)->GetEnv(vm, (void*)&env, JNI_VERSION_1_2); + if (isAttached == JNI_OK) { + *threadCreatedFromJava = true; + return env; + } else if (isAttached == JNI_EDETACHED) { + *threadCreatedFromJava = false; + rv = (*vm)->AttachCurrentThread(vm, (void*)&env, 0); + if (rv != 0) { + fprintf(stderr, "Call to AttachCurrentThread " + "failed with error: %d\n", rv); + return NULL; + } + } else { + *threadCreatedFromJava = false; + fprintf(stderr, "Call to GetEnv " + "failed with error: %d\n", isAttached); return NULL; } } @@ -823,7 +838,7 @@ } THREAD_LOCAL_STORAGE_SET_QUICK(state); - state->env = getGlobalJNIEnv(); + state->env = getGlobalJNIEnv(&state->threadCreatedFromJava); mutexUnlock(&jvmMutex); if (!state->env) { diff -ru a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/thread_local_storage.c b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/thread_local_storage.c --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/thread_local_storage.c 2021-05-06 15:53:56.037724986 +0200 +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/thread_local_storage.c 2021-05-06 15:54:07.189692961 +0200 @@ -52,8 +52,8 @@ jthrowable jthr; char thr_name[MAXTHRID]; - /* Detach the current thread from the JVM */ - if (env) { + /* Detach the current thread from the JVM if this is a native thread */ + if (!state->threadCreatedFromJava && env) { ret = (*env)->GetJavaVM(env, &vm); if (ret != 0) { diff -ru a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/thread_local_storage.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/thread_local_storage.h --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/thread_local_storage.h 2021-05-06 15:53:56.037724986 +0200 +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/thread_local_storage.h 2021-05-06 15:54:07.189692961 +0200 @@ -28,6 +28,8 @@ #include +#include + /* * Most operating systems support the more efficient __thread construct, which * is initialized by the linker. The following macros use this technique on the @@ -52,6 +54,8 @@ #endif struct ThreadLocalState { + /* Was this thread created by Java or native? */ + bool threadCreatedFromJava; /* The JNIEnv associated with the current thread */ JNIEnv *env; /* The last exception stack trace that occured on this thread */