Skip to content

Commit

Permalink
feat: gracefully handle panics in plugins (#13329)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Dec 30, 2023
1 parent 71257c1 commit 3421505
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions crates/polars-plan/src/dsl/function_expr/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::ffi::CStr;
use std::process::abort;
use std::sync::RwLock;

use arrow::ffi::{import_field_from_c, ArrowSchema};
Expand Down Expand Up @@ -109,7 +108,7 @@ pub(super) unsafe fn call_plugin(
} else {
let msg = retrieve_error_msg(lib);
let msg = msg.to_string_lossy();
check_panic(msg.as_ref());
check_panic(msg.as_ref())?;
polars_bail!(ComputeError: "the plugin failed with message: {}", msg)
}
} else {
Expand Down Expand Up @@ -156,17 +155,15 @@ pub(super) unsafe fn plugin_field(
} else {
let msg = retrieve_error_msg(lib);
let msg = msg.to_string_lossy();
check_panic(msg.as_ref());
check_panic(msg.as_ref())?;
polars_bail!(ComputeError: "the plugin failed with message: {}", msg)
}
} else {
polars_bail!(ComputeError: "this polars engine doesn't support plugin version: {}", major)
}
}

fn check_panic(msg: &str) {
if msg == "PANIC" {
eprintln!("The plugin panicked which is unrecoverable. Polars will abort");
abort()
}
fn check_panic(msg: &str) -> PolarsResult<()> {
polars_ensure!(msg != "PANIC", ComputeError: "the plugin panicked\n\nThe message is suppressed. Set POLARS_VERBOSE=1 to send the panic message to stderr.");
Ok(())
}

0 comments on commit 3421505

Please sign in to comment.