philo-he commented on code in PR #13728:
URL: https://github.com/apache/gluten/pull/13728#discussion_r4676941386
##########
cpp/core/jni/JniCommon.h:
##########
@@ -114,6 +115,8 @@ static inline void
attachCurrentThreadAsDaemonOrThrow(JavaVM* vm, JNIEnv** out)
if (getEnvStat != JNI_OK) {
throw gluten::GlutenException("Failed to reattach current thread to
JVM.");
}
+ // Detach when this native thread exits, so the JVM is not left with a
JavaThread for a dead pthread.
+ gluten::detachCurrentThreadOnExit(vm);
Review Comment:
No. We register the exit detach only when
`attachCurrentThreadAsDaemonOrThrow` itself attaches the thread. A Java-created
thread is always attached, so it is never registered and the JVM keeps managing
it. This is same for native threads that Java or libhdfs attached first.
AI helped add some tests, and the test named
`doesNotDetachThreadAttachedElsewhere` covers this.
##########
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;
Review Comment:
Added some tests in the new revision.
##########
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) {
+ vm->DetachCurrentThread();
+ }
+}
+
+bool createExitDetachKey() {
+ const int rc = pthread_key_create(&exitDetachKey, detachOnThreadExit);
+ if (rc != 0) {
+ LOG(WARNING) << "pthread_key_create failed with " << rc
+ << "; native threads will stay attached to the JVM after they
exit";
+ }
+ return rc == 0;
+}
+
+} // namespace
+
+void detachCurrentThreadOnExit(JavaVM* vm) {
+ static const bool keyCreated = createExitDetachKey();
+ if (!keyCreated) {
+ return;
+ }
+ exitDetachVm.store(vm, std::memory_order_release);
+ pthread_setspecific(exitDetachKey, reinterpret_cast<void*>(kArmed));
Review Comment:
I let AI try `thread_local` + destructor, but it isn't safe with HDFS. glibc
runs all `thread_local` destructors before any pthread key destructor, so we
would always detach before libhdfs's destructor runs. In a test against a real
JVM, a thread that Gluten attached and libhdfs then used crashed inside the
libhdfs-like destructor, the GLUTEN-11895 failure.
Please feel free to let me know whether you have any suggestions. Thanks!
--
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]