guowangy commented on PR #13728: URL: https://github.com/apache/gluten/pull/13728#issuecomment-7130093763
> > Thanks for the fix. We may need to check how this works with the fix [#11896](https://github.com/apache/gluten/pull/11896)? > > [@zhztheplayer](https://github.com/zhztheplayer), [#11896](https://github.com/apache/gluten/pull/11896) removed the detach calls because libhdfs detaches its own threads at exit, from a pthread key destructor (`hdfsThreadDestructor`) that still uses its cached `JNIEnv*`. If we detached first, libhdfs would crash (GLUTEN-11895). > > This PR keeps that rule: it never detaches while a thread is running, and at thread exit it always lets libhdfs go first. Our destructor skips its first call and only acts on its second one, which glibc runs after every other key destructor, including libhdfs's. By then a thread that libhdfs used is already detached, so we do nothing; only threads used by Gluten alone get detached by us. > > `JniThreadExitTest` covers both orders of the two keys. Removing the "skip first call" step fails the test where libhdfs's key is newer, which is the GLUTEN-11895 case. Yes, it is compatible with #11896. libhdfs caches the `JNIEnv*` per thread, and uses it again in its exit destructor (`hdfsThreadDestructor`), but never check its validation. So we must never detach before libhdfs is done with the thread. For this PR, at exit, destructor (`detachOnThreadExit`) sets its value again on its first call, so glibc runs it again after all other key destructors (POSIX `pthread_key_create` rule). If libhdfs has already detached the thread, `GetEnv()` returns `JNI_EDETACHED` and it skips. Recently, HDFS has a fix to address this problem: [HDFS-16021](https://issues.apache.org/jira/browse/HDFS-16021) makes libhdfs detach only threads it attached. If that fix is available, we may not need such workaround. -- 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]
