fix(ffi): return parser status and isolate thread errors

This commit is contained in:
Jan Petykiewicz 2026-11-14 14:23:13 -08:00
commit 24438c4840
2 changed files with 234 additions and 24 deletions

20
tests/common/mod.rs Normal file
View file

@ -0,0 +1,20 @@
use klamath_rs_ext::{basic::encode_real8, records};
pub fn record(out: &mut Vec<u8>, tag: u16, data: &[u8]) {
out.extend_from_slice(&u16::try_from(data.len() + 4).unwrap().to_be_bytes());
out.extend_from_slice(&tag.to_be_bytes());
out.extend_from_slice(data);
}
pub fn library_header() -> Vec<u8> {
let mut bytes = Vec::new();
record(&mut bytes, records::RTAG_HEADER, &600_i16.to_be_bytes());
record(&mut bytes, records::RTAG_BGNLIB, &[0; 24]);
record(&mut bytes, records::RTAG_LIBNAME, b"test");
let units: Vec<u8> = [0.001, 1e-9]
.into_iter()
.flat_map(|v| encode_real8(v).unwrap().to_be_bytes())
.collect();
record(&mut bytes, records::RTAG_UNITS, &units);
bytes
}

86
tests/ffi_errors.rs Normal file
View file

@ -0,0 +1,86 @@
mod common;
use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
use klamath_rs_ext::{last_error_message, read_path, records};
use std::{ffi::{CStr, CString}, fs, path::PathBuf, ptr};
struct TestFile(PathBuf);
impl TestFile {
fn new(name: &str, contents: &[u8]) -> Self {
let path = std::env::temp_dir().join(format!("klamath-{name}-{}.gds", std::process::id()));
fs::write(&path, contents).unwrap();
Self(path)
}
fn cpath(&self) -> CString {
CString::new(self.0.to_str().unwrap()).unwrap()
}
}
impl Drop for TestFile {
fn drop(&mut self) {
let _ = fs::remove_file(&self.0);
}
}
fn error_message() -> String {
let message = last_error_message();
assert!(!message.is_null());
unsafe { CStr::from_ptr(message) }.to_str().unwrap().to_owned()
}
#[test]
fn path_errors_preserve_outputs_and_convert_parser_panics() {
let mut array = FFI_ArrowArray::empty();
let mut schema = FFI_ArrowSchema::empty();
assert_eq!(unsafe { read_path(ptr::null(), &mut array, &mut schema) }, -1);
assert!(error_message().contains("path pointer was null"));
assert!(array.is_released());
// A mismatched timestamp size currently makes Record::read panic.
let mut bytes = Vec::new();
common::record(&mut bytes, records::RTAG_HEADER, &600_i16.to_be_bytes());
common::record(&mut bytes, records::RTAG_BGNLIB, &[0; 12]);
let file = TestFile::new("invalid-timestamps", &bytes);
assert_eq!(unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) }, -1);
assert!(error_message().contains("Expected record size"));
assert!(array.is_released());
let mut bytes = common::library_header();
common::record(&mut bytes, records::RTAG_ENDLIB, &[]);
let file = TestFile::new("valid-empty", &bytes);
assert_eq!(unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) }, 0);
assert!(!array.is_released());
assert!(last_error_message().is_null());
}
#[test]
fn other_threads_do_not_clear_or_free_error_messages() {
let mut array = FFI_ArrowArray::empty();
let mut schema = FFI_ArrowSchema::empty();
assert_eq!(unsafe { read_path(ptr::null(), &mut array, &mut schema) }, -1);
let pointer = last_error_message();
let message = error_message();
std::thread::spawn(|| {
assert!(last_error_message().is_null());
let mut bytes = common::library_header();
common::record(&mut bytes, records::RTAG_ENDLIB, &[]);
let file = TestFile::new("thread-empty", &bytes);
let mut array = FFI_ArrowArray::empty();
let mut schema = FFI_ArrowSchema::empty();
assert_eq!(unsafe { read_path(ptr::null(), &mut array, &mut schema) }, -1);
assert_eq!(unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) }, 0);
assert!(last_error_message().is_null());
}).join().unwrap();
assert_eq!(last_error_message(), pointer);
assert_eq!(unsafe { CStr::from_ptr(pointer) }.to_str().unwrap(), message);
}
#[test]
fn null_arrow_outputs_return_an_error() {
assert_eq!(unsafe { read_path(ptr::null(), ptr::null_mut(), ptr::null_mut()) }, -1);
assert!(error_message().contains("Arrow output pointer was null"));
}