if a non-main thread panics, we now log its panic message to stderr
@@ -7,6 +7,7 @@ use crate::FlushErrorState;
use std::any::Any;
use std::cell::Cell;
use std::panic::catch_unwind;
+use std::sync::{Arc, Mutex};
extern "C" {
fn pg_re_throw();
@@ -65,7 +66,22 @@ fn take_panic_location() -> PanicLocation {
}
pub fn register_pg_guard_panic_handler() {
+ // it's expected that this function will only ever be called by `pg_module_magic!()` in the
+ // main thread
+ thread_local! { static IS_MAIN_THREAD: Arc<Mutex<Option<()>>> = Arc::new(Mutex::new(None)); }
+ IS_MAIN_THREAD.with(|v| v.lock().as_mut().unwrap().replace(()));
+
std::panic::set_hook(Box::new(|info| {
+ if IS_MAIN_THREAD.with(|v| v.lock().as_ref().unwrap().is_none()) {
+ // if a thread that isn't the main thread panic'd, we make a best effort to push
+ // a message to stderr, which hopefully Postgres is logging somewhere
+ eprintln!(
+ "thread={:?}, id={:?}, {}",
+ std::thread::current().name(),
+ std::thread::current().id(),
+ info
+ );
+ } else {
PANIC_LOCATION.with(|p| {
let existing = p.take();
@@ -82,6 +98,7 @@ pub fn register_pg_guard_panic_handler() {
existing
})
});
+ }
}))
}
