fix: wrap contour detection and pipeline in catch_unwind

imageproc::find_contours panics on some edge images (called
Option::unwrap() on None). Wrap with catch_unwind and provide
a safe fallback (use full image bounds).

Also add catch_unwind around the entire pipeline so no panic
can crash the worker thread.

Co-Authored-By: Kilo <kilo@kilo.ai>
This commit is contained in:
asepharyana
2026-07-24 19:01:28 +07:00
co-authored by Kilo
parent d2bfcb83a0
commit 5a64048ba2
2 changed files with 66 additions and 23 deletions
+22
View File
@@ -26,10 +26,32 @@ pub struct ScanResult {
}
/// Run the full scanner pipeline with all stages.
/// Wrapped in catch_unwind to prevent imageproc panics from killing the worker.
pub async fn process(
job: &Job,
config: &WorkerConfig,
progress: &ProgressReporter,
) -> Result<ScanResult, Box<dyn std::error::Error + Send + Sync>> {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
process_inner(job, config, progress)
}));
match result {
Ok(fut) => fut.await,
Err(panic) => {
let msg = panic
.downcast_ref::<&str>()
.unwrap_or(&"Unknown panic in scanner pipeline");
Err(format!("Pipeline panicked: {}", msg).into())
}
}
}
/// Inner pipeline implementation (runs inside catch_unwind).
async fn process_inner(
job: &Job,
config: &WorkerConfig,
progress: &ProgressReporter,
) -> Result<ScanResult, Box<dyn std::error::Error + Send + Sync>> {
let start = Instant::now();
let input_path = Path::new(&job.file_path);