viirya opened a new pull request, #7042:
URL: https://github.com/apache/datafusion-comet/pull/7042

   ## Which issue does this PR close?
   
   Closes #7041.
   
   ## Rationale for this change
   
   The panic hook stores the backtrace of every native panic in one 
process-wide slot, and the JNI boundary takes whatever the slot holds when a 
panic becomes a `CometNativeException`. When two threads panic at about the 
same time, one exception can carry the other thread's stack trace and the other 
none, so the stack trace points at code that did not fail. Panics that Comet 
catches itself (for example in the variant casts) also write to the slot, and 
another thread's boundary can take their backtrace.
   
   ## What changes are included in this PR?
   
   `PANIC_BACKTRACE` in `native/jni-bridge/src/errors.rs` becomes a 
thread-local `Cell<Option<String>>` instead of a global `Mutex`. The panic hook 
writes the current thread's slot and `NativeFailure::from` takes it. Every 
place that turns a panic into `CometError::Panic` (`try_unwrap_or_throw`, 
`catch_native`, and the stream drop in `BatchProducer::stop`) does so on the 
thread that panicked, so the backtrace is always in that thread's slot.
   
   The hook uses `try_with`, because a panic inside a panic hook aborts the 
process and the thread-local is gone while its thread is being torn down.
   
   ## How are these changes tested?
   
   `panic_backtrace_stays_with_its_thread` makes two threads panic inside 
`catch_native`. Each thread drops a value while it unwinds that waits until 
both panic hooks have run, so neither takes its backtrace before the other 
thread's hook has written. The test checks that each failure carries the 
backtrace of its own panic. It fails on the current code (one thread gets no 
backtrace or the other thread's) and passes with this change.
   
   The existing JVM-backed tests in `errors.rs`, including the 
panic-to-exception test, pass unchanged.
   
   This pull request and its description were written by Isaac.
   


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