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

chore: friendly OOM message #223

Merged
merged 1 commit into from
Jan 5, 2024
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
12 changes: 12 additions & 0 deletions src/bgworker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ pub mod upgrade;
pub unsafe fn init() {
use pgrx::bgworkers::BackgroundWorkerBuilder;
use pgrx::bgworkers::BgWorkerStartTime;
use std::time::Duration;
BackgroundWorkerBuilder::new("vectors")
.set_function("vectors_main")
.set_library("vectors")
.set_argument(None)
.enable_shmem_access(None)
.set_start_time(BgWorkerStartTime::PostmasterStart)
.set_restart_time(Some(Duration::from_secs(1)))
.load();
}

Expand All @@ -19,6 +21,9 @@ extern "C" fn vectors_main(_arg: pgrx::pg_sys::Datum) {
}

pub fn main() {
pub struct AllocErrorPanicPayload {
pub layout: std::alloc::Layout,
}
{
let mut builder = env_logger::builder();
builder.target(env_logger::Target::Stderr);
Expand All @@ -33,6 +38,10 @@ pub fn main() {
builder.init();
}
std::panic::set_hook(Box::new(|info| {
if let Some(oom) = info.payload().downcast_ref::<AllocErrorPanicPayload>() {
log::error!("Out of memory. Layout: {:?}.", oom.layout);
return;
}
let backtrace;
#[cfg(not(debug_assertions))]
{
Expand All @@ -44,6 +53,9 @@ pub fn main() {
}
log::error!("Panickied. Info: {:?}. Backtrace: {}.", info, backtrace);
}));
std::alloc::set_alloc_error_hook(|layout| {
cutecutecat marked this conversation as resolved.
Show resolved Hide resolved
std::panic::panic_any(AllocErrorPanicPayload { layout });
});
use service::worker::Worker;
use std::path::Path;
let path = Path::new("pg_vectors");
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! Provides an easy-to-use extension for vector similarity search.
#![feature(never_type)]
#![feature(alloc_error_hook)]

mod bgworker;
mod datatype;
Expand Down
Loading