philo-he opened a new pull request, #13728:
URL: https://github.com/apache/gluten/pull/13728

   ## What changes are proposed in this pull request?
   
   Native threads that Gluten attaches to the JVM are never detached. When one 
exits, the JVM keeps a `JavaThread` for a dead pthread, and the next 
thread-list walk crashes the executor:
   
   ```
   # Problematic frame:
   # C  [libc.so.6+0x8c924]  pthread_getcpuclockid+0x4
   V  [libjvm.so]  Thread::print_on(outputStream*, bool) const
   VM_Operation: PrintThreads, requested by "Attach Listener"
   ```
   
   #11896 (GLUTEN-11895) stopped calling `DetachCurrentThread` in the JNI 
helpers, because detaching breaks libhdfs's cached `JNIEnv*`, and added 
`JniAwareThreadFactory` to detach spill-pool threads instead. #11882, merged 
after it, moved the pools into `VeloxBackend` and removed 
`JniAwareThreadFactory` along with its only use, so since 1.7.0 no 
Gluten-attached thread is ever detached. Simply restoring the factory is not 
enough: on the IO pool it is unsafe with HDFS, and it does not cover the 
broadcast hash-table build pool added in #13115.
   
   This PR:
   
   - Adds `gluten::detachCurrentThreadOnExit(JavaVM*)` 
(`cpp/core/jni/JniThreadExit.{h,cc}`), which registers a pthread TSD destructor 
for the calling thread.
   - Calls it from `attachCurrentThreadAsDaemonOrThrow` whenever that helper 
attaches a thread, covering every Gluten-attached thread regardless of pool.
   - Defers the destructor by one round before calling `DetachCurrentThread`, 
so libhdfs's `hdfsThreadDestructor` always runs first. Threads used by libhdfs 
are detached by libhdfs, and GLUTEN-11895 cannot regress.
   
   No mid-life detach, and no HDFS build guard is needed.
   
   ## How was this issue surfaced, and how to reproduce it?
   
   Found while benchmarking 1.8.0-rc1 with TPC-H/TPC-DS SF3000, Parquet on S3. 
11 executors crashed in TPC-H and 20 in TPC-DS, all with the stack above. Each 
crash happened while an external process had attached to the executor JVM and 
requested a thread dump (`PrintThreads` from the `Attach Listener` thread); on 
our side, the node's log collection is one such process. 
`-XX:+DisableAttachMechanism` avoids this trigger but leaves the dangling 
threads in place.
   Conditions:
   
   1. Threads attached only by Gluten: S3/local scans, spill and 
broadcast-build pools. On HDFS scans, libhdfs detaches IO threads itself, which 
hides the bug.
   2. Those threads exit: the executor idles for more than `--threadtimeout_ms` 
(60 s).
   3. Something walks the JVM thread list: `jstack`, `jcmd Thread.print`, JVMTI 
agents, profilers, log collectors.
   4. For a SIGSEGV, the dead thread's stack must be unmapped; 
`GLIBC_TUNABLES=glibc.pthread.stack_cache_size=0` makes this likely.
   
   Repro: in local-mode `spark-shell` with Gluten, run some TPC-H queries, wait 
more than 60 s, run them again, then `jcmd <pid> Thread.print`. Before the 
patch, some `"Thread-N"` entries with no Java frames have a `nid` that no 
longer exists under `/proc/<pid>/task/`; after it, none do.
   
   ## How was this patch tested?
   
   Local tests verified.
   
   ## Was this patch authored or co-authored using generative AI tooling?
   Generated-by: Claude Opus 5.5


-- 
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