Skip to content

Commit

Permalink
chore: friendly OOM message
Browse files Browse the repository at this point in the history
Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi committed Jan 2, 2024
1 parent e530742 commit 79b68f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/bgworker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
pub mod normal;
pub mod upgrade;

pub struct OomError {
pub layout: std::alloc::Layout,
}

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 @@ -33,6 +39,10 @@ pub fn main() {
builder.init();
}
std::panic::set_hook(Box::new(|info| {
if let Some(oom) = info.payload().downcast_ref::<OomError>() {
log::error!("Out of memory. Layout: {:?}.", oom.layout);
return;
}
let backtrace;
#[cfg(not(debug_assertions))]
{
Expand All @@ -44,6 +54,9 @@ pub fn main() {
}
log::error!("Panickied. Info: {:?}. Backtrace: {}.", info, backtrace);
}));
std::alloc::set_alloc_error_hook(|layout| {
std::panic::panic_any(OomError { 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 @@ -3,6 +3,7 @@
//! Provides an easy-to-use extension for vector similarity search.
#![feature(offset_of)]
#![feature(never_type)]
#![feature(alloc_error_hook)]

mod bgworker;
mod datatype;
Expand Down

0 comments on commit 79b68f1

Please sign in to comment.