diff --git a/exts/rag_bge_small_en_v15/src/lib.rs b/exts/rag_bge_small_en_v15/src/lib.rs index 2ce8919..61483dc 100644 --- a/exts/rag_bge_small_en_v15/src/lib.rs +++ b/exts/rag_bge_small_en_v15/src/lib.rs @@ -18,7 +18,7 @@ use rayon::{ThreadPool, ThreadPoolBuilder}; use std::{fs, os::unix::fs::PermissionsExt, sync::OnceLock}; use tokio::{ net::UnixListener, - time::{sleep, Duration}, + time::Duration, }; use tokio_stream::wrappers::UnixListenerStream; use tonic::{transport::Server, Request, Response, Status}; @@ -167,8 +167,23 @@ pub extern "C" fn background_main(arg: pg_sys::Datum) { .add_service(EmbeddingGeneratorServer::new(embedder)) .serve_with_incoming_shutdown(uds_stream, async { while !BackgroundWorker::sigterm_received() { - sleep(Duration::from_millis(500)).await; - } + unsafe { + let mask = pg_sys::WL_EXIT_ON_PM_DEATH | pg_sys::WL_LATCH_SET; + let my_latch = &mut (*pg_sys::MyProc).procLatch as *mut pg_sys::Latch; + let events = pg_sys::WaitLatch( + my_latch, + mask as i32, + 0, + pg_sys::PG_WAIT_EXTENSION, + ); + pg_sys::ResetLatch(my_latch); + + if (events & pg_sys::WL_EXIT_ON_PM_DEATH as i32) != 0 { + // postmaster died + pg_sys::proc_exit(1); + } + } + } }) .await .expect_or_pg_err("Couldn't create server"); diff --git a/exts/rag_jina_reranker_v1_tiny_en/src/lib.rs b/exts/rag_jina_reranker_v1_tiny_en/src/lib.rs index 3ea96b4..84d18d1 100644 --- a/exts/rag_jina_reranker_v1_tiny_en/src/lib.rs +++ b/exts/rag_jina_reranker_v1_tiny_en/src/lib.rs @@ -17,7 +17,7 @@ use reranking::{ use std::{fs, os::unix::fs::PermissionsExt, sync::OnceLock}; use tokio::{ net::UnixListener, - time::{sleep, Duration}, + time::Duration, }; use tokio_stream::wrappers::UnixListenerStream; use tonic::{transport::Server, Request, Response, Status}; @@ -171,7 +171,22 @@ pub extern "C" fn background_main(arg: pg_sys::Datum) { .add_service(RerankerServer::new(reranker)) .serve_with_incoming_shutdown(uds_stream, async { while !BackgroundWorker::sigterm_received() { - sleep(Duration::from_millis(500)).await; + unsafe { + let mask = pg_sys::WL_EXIT_ON_PM_DEATH | pg_sys::WL_LATCH_SET; + let my_latch = &mut (*pg_sys::MyProc).procLatch as *mut pg_sys::Latch; + let events = pg_sys::WaitLatch( + my_latch, + mask as i32, + 0, + pg_sys::PG_WAIT_EXTENSION, + ); + pg_sys::ResetLatch(my_latch); + + if (events & pg_sys::WL_EXIT_ON_PM_DEATH as i32) != 0 { + // postmaster died + pg_sys::proc_exit(1); + } + } } }) .await