Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(audio): stop fbank panics from crashing the speaker-embedding worker (#4159)

Louis Beaumont6ab68644d2cf
diff --git a/crates/screenpipe-audio/src/speaker/embedding.rs b/crates/screenpipe-audio/src/speaker/embedding.rs
index ca350af44..6c95f3215 100644
--- a/crates/screenpipe-audio/src/speaker/embedding.rs
+++ b/crates/screenpipe-audio/src/speaker/embedding.rs
@@ -26,9 +26,18 @@ impl EmbeddingExtractor {
pub fn compute(&mut self, samples: &[f32]) -> Result<impl Iterator<Item = f32>> {
// knf-rs exposes ndarray 0.16 types; the workspace is on 0.17 for
// ort rc.12 — rebuild the array in our ndarray version via raw parts.
- let features_016 = knf_rs::compute_fbank(samples)
- .map_err(anyhow::Error::msg)
- .context("compute_fbank failed")?;
+ //
+ // knf-rs also *panics* (Option::unwrap on None deep in
+ // OnlineGenericBaseFeature::InputFinished) when `samples` is too short
+ // to yield an fbank frame, unwinding the audio worker instead of
+ // returning an error — the highest-volume crash in the field
+ // (SCREENPIPE-CLI-3S). Run it under the same panic guard create_session
+ // uses so the caller skips the segment gracefully (see get_speaker_embedding).
+ let features_016 = super::catch_panic_into_error("compute_fbank", || {
+ knf_rs::compute_fbank(samples)
+ .map_err(anyhow::Error::msg)
+ .context("compute_fbank failed")
+ })?;
let (rows, cols) = features_016.dim();
let features: Array2<f32> =
Array2::from_shape_vec((rows, cols), features_016.into_raw_vec())
diff --git a/crates/screenpipe-audio/src/speaker/mod.rs b/crates/screenpipe-audio/src/speaker/mod.rs
index 8429bec48..04b272b4c 100644
--- a/crates/screenpipe-audio/src/speaker/mod.rs
+++ b/crates/screenpipe-audio/src/speaker/mod.rs
@@ -14,7 +14,7 @@ pub fn create_session<P: AsRef<Path>>(path: P) -> Result<ort::session::Session>
// `expect("Failed to initialize ORT API")` at lib.rs:188). That panic
// bubbles up the tokio worker and Sentry — convert it to a normal error
// so callers fall back gracefully instead of crashing the runtime.
- catch_panic_into_error(|| {
+ catch_panic_into_error("ort session init", || {
// ort rc.12: builder ops return `Error<SessionBuilder>` (recovery
// payload, not Send+Sync) — convert via Display for anyhow.
let oe = |e: &dyn std::fmt::Display| anyhow!("ort: {e}");
@@ -29,7 +29,13 @@ pub fn create_session<P: AsRef<Path>>(path: P) -> Result<ort::session::Session>
})
}
-fn catch_panic_into_error<F, T>(f: F) -> Result<T>
+/// Run `f`, converting any panic it unwinds into an error tagged with
+/// `context`. Both ort (FFI) and knf-rs (fbank) `unwrap()`/`expect()` deep in
+/// native bindings on bad input or environment mismatches; without this guard
+/// those panics unwind the audio/tokio worker and crash the runtime instead of
+/// being handled. The `context` names which guarded call failed so logs and
+/// Sentry stay legible (e.g. "ort session init" vs "compute_fbank").
+fn catch_panic_into_error<F, T>(context: &str, f: F) -> Result<T>
where
F: FnOnce() -> Result<T>,
{
@@ -41,7 +47,7 @@ where
.map(|s| (*s).to_string())
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "unknown panic".to_string());
- Err(anyhow!("ort session init panicked: {}", msg))
+ Err(anyhow!("{context} panicked: {msg}"))
}
}
}
@@ -99,13 +105,14 @@ mod tests {
#[test]
fn catch_panic_into_error_passes_through_ok() {
- let r: Result<i32> = catch_panic_into_error(|| Ok(7));
+ let r: Result<i32> = catch_panic_into_error("ort session init", || Ok(7));
assert_eq!(r.unwrap(), 7);
}
#[test]
fn catch_panic_into_error_passes_through_err() {
- let r: Result<()> = catch_panic_into_error(|| Err(anyhow!("normal failure")));
+ let r: Result<()> =
+ catch_panic_into_error("ort session init", || Err(anyhow!("normal failure")));
let msg = r.unwrap_err().to_string();
assert!(msg.contains("normal failure"));
assert!(!msg.contains("panicked"));
@@ -113,7 +120,7 @@ mod tests {
#[test]
fn catch_panic_into_error_catches_str_panic() {
- let r: Result<()> = catch_panic_into_error(|| panic!("boom"));
+ let r: Result<()> = catch_panic_into_error("ort session init", || panic!("boom"));
let msg = r.unwrap_err().to_string();
assert!(msg.contains("ort session init panicked"));
assert!(msg.contains("boom"));
@@ -121,7 +128,8 @@ mod tests {
#[test]
fn catch_panic_into_error_catches_string_panic() {
- let r: Result<()> = catch_panic_into_error(|| panic!("formatted: {}", 42));
+ let r: Result<()> =
+ catch_panic_into_error("ort session init", || panic!("formatted: {}", 42));
let msg = r.unwrap_err().to_string();
assert!(msg.contains("ort session init panicked"));
assert!(msg.contains("42"));
@@ -131,12 +139,37 @@ mod tests {
fn catch_panic_into_error_simulates_ort_api_init_panic() {
// Mirrors the exact panic ort 2.0.0-rc.10 raises at lib.rs:188 when
// `NonNull::new(api).expect("Failed to initialize ORT API")` triggers.
- let r: Result<()> = catch_panic_into_error(|| panic!("Failed to initialize ORT API"));
+ let r: Result<()> = catch_panic_into_error("ort session init", || {
+ panic!("Failed to initialize ORT API")
+ });
let msg = r.unwrap_err().to_string();
assert!(msg.contains("ort session init panicked"));
assert!(msg.contains("Failed to initialize ORT API"));
}
+ #[test]
+ fn catch_panic_into_error_uses_supplied_context() {
+ // The context tag distinguishes which guarded call panicked. embedding.rs
+ // passes "compute_fbank"; make sure that label reaches the error.
+ let r: Result<()> = catch_panic_into_error("compute_fbank", || panic!("boom"));
+ let msg = r.unwrap_err().to_string();
+ assert!(msg.contains("compute_fbank panicked"), "got: {msg}");
+ assert!(msg.contains("boom"));
+ }
+
+ #[test]
+ fn compute_fbank_tiny_input_is_contained_by_guard() {
+ // SCREENPIPE-CLI-3S regression: knf-rs unwraps a None deep in
+ // OnlineGenericBaseFeature::InputFinished for buffers too short to yield
+ // a frame, unwinding the audio worker. Routing compute_fbank through the
+ // guard must contain that. Reaching the end of this test (no unwind) is
+ // the assertion; the result may be Ok or a handled Err depending on knf.
+ let guarded = catch_panic_into_error("compute_fbank", || {
+ knf_rs::compute_fbank(&[0.0f32; 16]).map_err(anyhow::Error::msg)
+ });
+ let _ = guarded;
+ }
+
#[test]
fn create_session_returns_err_for_missing_path() {
// Sanity-check the normal error path still flows through ?-propagation