Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dubious thread assertion #562

Merged
merged 2 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions pgx-pg-sys/src/submodules/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,8 @@ fn take_panic_location() -> PanicLocation {
})
}

// via pg_module_magic!() this gets set to Some(()) for the "main" thread, and remains at None
// for all other threads.
#[cfg(debug_assertions)]
thread_local! { pub(crate) static IS_MAIN_THREAD: once_cell::sync::OnceCell<()> = once_cell::sync::OnceCell::new() }

pub fn register_pg_guard_panic_handler() {
// first, lets ensure we're not calling ourselves twice
#[cfg(debug_assertions)]
{
if IS_MAIN_THREAD.with(|v| v.get().is_some()) {
panic!("IS_MAIN_THREAD has already been set")
}

// it's expected that this function will only ever be called by `pg_module_magic!()` by the main thread
IS_MAIN_THREAD.with(|v| v.set(()).expect("failed to set main thread sentinel"));
}

std::panic::set_hook(Box::new(|info| {
#[cfg(debug_assertions)]
{
if IS_MAIN_THREAD.with(|v| v.get().is_none()) {
// 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
);
}
}

PANIC_LOCATION.with(|p| {
let existing = p.take();

Expand Down
10 changes: 3 additions & 7 deletions pgx-pg-sys/src/submodules/setjmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
///
/// Currently, this function is only used by pgx' generated Postgres bindings. It is not (yet)
/// intended (or even necessary) for normal user code.
///
/// Calling this function from anything but the main thread can result in unpredictable behavior.
#[inline(always)]
pub(crate) unsafe fn pg_guard_ffi_boundary<T, F: FnOnce() -> T>(f: F) -> T {
use crate as pg_sys;

// as the panic message says, we can't call Postgres functions from threads
// the value of IS_MAIN_THREAD gets set through the pg_module_magic!() macro
#[cfg(debug_assertions)]
if crate::submodules::guard::IS_MAIN_THREAD.with(|v| v.get().is_none()) {
panic!("functions under #[pg_guard] cannot be called from threads");
};

// This should really, really not be done in a multithreaded context
let prev_exception_stack = pg_sys::PG_exception_stack;
let prev_error_context_stack = pg_sys::error_context_stack;
let mut jump_buffer = std::mem::MaybeUninit::uninit();
Expand Down