Copilot commented on code in PR #13728: URL: https://github.com/apache/gluten/pull/13728#discussion_r4662273847
########## 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: No automated regression tests are added for the two-pass exit hook, leaving the leaked attachment and early-detach failure modes unprotected. Please add registered native-thread tests that verify attachment remains valid during worker execution, detachment occurs during exit, and already-attached callers retain ownership. Simulate libhdfs cleanup with its pthread key allocated both before and after Gluten's key, checking that the cached `JNIEnv*` remains valid during cleanup and detachment happens exactly once. -- 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]
