Skip to content

Commit 79b68f1

Browse files
committed
chore: friendly OOM message
Signed-off-by: usamoi <[email protected]>
1 parent e530742 commit 79b68f1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/bgworker/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
pub mod normal;
22
pub mod upgrade;
33

4+
pub struct OomError {
5+
pub layout: std::alloc::Layout,
6+
}
7+
48
pub unsafe fn init() {
59
use pgrx::bgworkers::BackgroundWorkerBuilder;
610
use pgrx::bgworkers::BgWorkerStartTime;
11+
use std::time::Duration;
712
BackgroundWorkerBuilder::new("vectors")
813
.set_function("vectors_main")
914
.set_library("vectors")
1015
.set_argument(None)
1116
.enable_shmem_access(None)
1217
.set_start_time(BgWorkerStartTime::PostmasterStart)
18+
.set_restart_time(Some(Duration::from_secs(1)))
1319
.load();
1420
}
1521

@@ -33,6 +39,10 @@ pub fn main() {
3339
builder.init();
3440
}
3541
std::panic::set_hook(Box::new(|info| {
42+
if let Some(oom) = info.payload().downcast_ref::<OomError>() {
43+
log::error!("Out of memory. Layout: {:?}.", oom.layout);
44+
return;
45+
}
3646
let backtrace;
3747
#[cfg(not(debug_assertions))]
3848
{
@@ -44,6 +54,9 @@ pub fn main() {
4454
}
4555
log::error!("Panickied. Info: {:?}. Backtrace: {}.", info, backtrace);
4656
}));
57+
std::alloc::set_alloc_error_hook(|layout| {
58+
std::panic::panic_any(OomError { layout });
59+
});
4760
use service::worker::Worker;
4861
use std::path::Path;
4962
let path = Path::new("pg_vectors");

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! Provides an easy-to-use extension for vector similarity search.
44
#![feature(offset_of)]
55
#![feature(never_type)]
6+
#![feature(alloc_error_hook)]
67

78
mod bgworker;
89
mod datatype;

0 commit comments

Comments
 (0)