zhouyuan commented on code in PR #13728:
URL: https://github.com/apache/gluten/pull/13728#discussion_r4663978114


##########
cpp/core/jni/JniThreadExit.cc:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "jni/JniThreadExit.h"
+
+#include <pthread.h>
+
+#include <atomic>
+#include <cstdint>
+
+#include <glog/logging.h>
+
+namespace gluten {
+namespace {
+
+// The TSD value of a thread whose detach is pending. A destructor that finds 
kArmed has run in the
+// first destructor round; it re-arms with kDeferred so that it is called 
again in the next round.
+constexpr uintptr_t kArmed = 1;
+constexpr uintptr_t kDeferred = 2;
+
+std::atomic<JavaVM*> exitDetachVm{nullptr};
+pthread_key_t exitDetachKey;
+
+// Why the detach waits one destructor round:
+//
+// libhdfs caches the JNIEnv* of each thread it uses and detaches that thread 
from its own TSD
+// destructor, which still dereferences the cached JNIEnv*. If this destructor 
detached first, libhdfs
+// would use a freed JNIEnv* (the crash fixed by GLUTEN-11895, which is why 
Gluten stopped detaching
+// in its JNI helpers). POSIX re-runs TSD destructors for keys whose values 
are non-NULL again after a
+// round, and glibc does so for up to PTHREAD_DESTRUCTOR_ITERATIONS (4) 
rounds. Deferring by one round
+// means libhdfs's destructor has run, whatever its key's position, before 
this one acts: the thread
+// is then already detached (GetEnv returns JNI_EDETACHED and nothing is done) 
or libhdfs never owned
+// it and this destructor detaches it.
+//
+// Detaching from a TSD destructor is supported by HotSpot: its own TSD 
destructor restores the
+// current-thread pointer so that later destructors can still call 
DetachCurrentThread.
+void detachOnThreadExit(void* value) {
+  if (reinterpret_cast<uintptr_t>(value) == kArmed) {
+    pthread_setspecific(exitDetachKey, reinterpret_cast<void*>(kDeferred));
+    return;
+  }
+  JavaVM* vm = exitDetachVm.load(std::memory_order_acquire);
+  JNIEnv* env = nullptr;
+  if (vm != nullptr && vm->GetEnv(reinterpret_cast<void**>(&env), 
JNI_VERSION_1_8) == JNI_OK) {

Review Comment:
   it seems JDK8 is specified here, do you mean JDK8 is not impacted?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to