Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

engine: last-resort guard around commands and background jobs

Brandon Thomas75ed688e09f0
diff --git a/crates/engine/src/export_tools.rs b/crates/engine/src/export_tools.rs
index 3269c8e5a..6d319ab1f 100644
--- a/crates/engine/src/export_tools.rs
+++ b/crates/engine/src/export_tools.rs
@@ -387,6 +387,7 @@ pub fn spawn_export(s: &mut Session, project: Arc<Project>, seq: ItemId, mut set
}
*res.lock().unwrap_or_else(|x| x.into_inner()) = Some(r);
};
+ let run = crate::export_tools::guard_job(job.progress.clone(), job.result.clone(), run);
s.jobs.push(job);
if wait || cfg!(target_arch = "wasm32") {
run();
@@ -396,6 +397,24 @@ pub fn spawn_export(s: &mut Session, project: Arc<Project>, seq: ItemId, mut set
Ok(id)
}
+/// Last-resort guard for a background job body (export, previews, proxies, …): a panic marks the
+/// job failed with an error instead of killing its thread and leaving the job "running" forever.
+/// The panic hook has already logged where it happened.
+pub(crate) fn guard_job(
+ progress: Arc<filmcraft_export::Progress>,
+ result: Arc<std::sync::Mutex<Option<std::result::Result<filmcraft_export::Report, String>>>>,
+ run: impl FnOnce() + Send + 'static,
+) -> impl FnOnce() + Send + 'static {
+ move || {
+ if std::panic::catch_unwind(std::panic::AssertUnwindSafe(run)).is_err() {
+ let msg = "internal error (see the crash log); the job was stopped".to_string();
+ *progress.error.lock().unwrap_or_else(|x| x.into_inner()) = Some(msg.clone());
+ progress.finished.store(true, Ordering::Relaxed);
+ *result.lock().unwrap_or_else(|x| x.into_inner()) = Some(Err(msg));
+ }
+ }
+}
+
fn file_name(path: &str) -> String {
Path::new(path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.to_string())
}
@@ -1010,3 +1029,20 @@ pub(crate) fn commands() -> Vec<CommandSpec> {
spec("export.quick", "Quick Export", r#"{"preset":str?,"path":str?,"wait":bool?, …settings params}"#, has_seq, quick, true),
]
}
+
+#[cfg(test)]
+mod guard_tests {
+ use super::*;
+
+ /// A panicking job body must leave the job finished with an error, not "running" forever.
+ #[test]
+ fn panicking_job_is_reported_as_failed() {
+ let progress: Arc<filmcraft_export::Progress> = Arc::default();
+ let result: Arc<std::sync::Mutex<Option<std::result::Result<filmcraft_export::Report, String>>>> = Arc::default();
+ let run = guard_job(progress.clone(), result.clone(), || panic!("job bug"));
+ std::thread::spawn(run).join().expect("the guard catches the panic");
+ assert!(progress.finished.load(Ordering::Relaxed));
+ assert!(progress.error.lock().unwrap().is_some());
+ assert!(matches!(&*result.lock().unwrap(), Some(Err(_))));
+ }
+}
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 873ba24e9..b6f40bb1a 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -599,7 +599,11 @@ impl Session {
return Err(e);
}
self.exec_depth += 1;
- let r = (spec.run)(self, &params);
+ // Last-resort guard: a panic in a command (a bug that escaped the never-crash rules)
+ // becomes an error for the caller (UI, CLI, control channel, MCP) instead of taking the
+ // session and its unsaved project down. The panic hook has logged where it happened.
+ let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.run)(self, &params)))
+ .unwrap_or_else(|_| Err(EngineError::Other(format!("internal error in `{id}` (see the crash log); the command did not complete"))));
self.exec_depth -= 1;
// source graphics: an edited instance updates the shared layers and the other instances
if r.is_ok() && spec.journal && id.starts_with("graphics.") && !self.project.source_graphics.is_empty() {
diff --git a/crates/engine/src/masks.rs b/crates/engine/src/masks.rs
index 1871aeeda..40ab2e458 100644
--- a/crates/engine/src/masks.rs
+++ b/crates/engine/src/masks.rs
@@ -634,6 +634,7 @@ fn track(s: &mut Session, p: &Value) -> Result<Value> {
prog.finished.store(true, Ordering::Relaxed);
*lock(&res) = Some(r);
};
+ let run = crate::export_tools::guard_job(job.progress.clone(), job.result.clone(), run);
s.jobs.push(job);
s.mask_jobs.push(PendingTrack { job: id, target: MaskSel { clip, effect: ei, mask: mi }, seq: seq_id, keys, applied: 0 });
let wait = crate::commands::bool_p(p, "wait").unwrap_or(false);
diff --git a/crates/engine/src/previews.rs b/crates/engine/src/previews.rs
index 5651f9b77..473030cd9 100644
--- a/crates/engine/src/previews.rs
+++ b/crates/engine/src/previews.rs
@@ -543,6 +543,7 @@ pub fn render(s: &mut Session, mode: RenderMode, p: &Value) -> Result<Value> {
prog.finished.store(true, Ordering::Relaxed);
*res.lock().unwrap_or_else(|x| x.into_inner()) = Some(r);
};
+ let run = crate::export_tools::guard_job(job.progress.clone(), job.result.clone(), run);
s.jobs.push(job);
let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(false);
if wait || cfg!(target_arch = "wasm32") {
@@ -629,6 +630,7 @@ pub fn render_audio(s: &mut Session, p: &Value) -> Result<Value> {
prog.finished.store(true, Ordering::Relaxed);
*res.lock().unwrap_or_else(|x| x.into_inner()) = Some(r);
};
+ let run = crate::export_tools::guard_job(job.progress.clone(), job.result.clone(), run);
s.jobs.push(job);
if p.get("wait").and_then(Value::as_bool).unwrap_or(false) || cfg!(target_arch = "wasm32") {
run();
diff --git a/crates/engine/src/project_manager.rs b/crates/engine/src/project_manager.rs
index f03191603..756c3b4e8 100644
--- a/crates/engine/src/project_manager.rs
+++ b/crates/engine/src/project_manager.rs
@@ -229,6 +229,7 @@ pub fn run(s: &mut Session, p: &Value) -> Result<Value> {
prog.finished.store(true, Ordering::Relaxed);
*res.lock().unwrap_or_else(|x| x.into_inner()) = Some(r);
};
+ let run = crate::export_tools::guard_job(job.progress.clone(), job.result.clone(), run);
s.jobs.push(job);
if bool_p(p, "wait").unwrap_or(false) || cfg!(target_arch = "wasm32") {
run();
diff --git a/crates/engine/src/proxies.rs b/crates/engine/src/proxies.rs
index 1c0db1464..55d131ee4 100644
--- a/crates/engine/src/proxies.rs
+++ b/crates/engine/src/proxies.rs
@@ -241,6 +241,7 @@ pub fn start_job(s: &mut Session, label: String, work: Vec<(ItemId, String, &'st
prog.finished.store(true, std::sync::atomic::Ordering::Relaxed);
*res.lock().unwrap_or_else(|x| x.into_inner()) = Some(r);
};
+ let run = crate::export_tools::guard_job(job.progress.clone(), job.result.clone(), run);
s.jobs.push(job);
s.media_jobs.push(PendingJob { job: id, on_done, outputs });
if wait || cfg!(target_arch = "wasm32") {
diff --git a/crates/engine/src/scene_detect.rs b/crates/engine/src/scene_detect.rs
index f2f823a5b..e38a06496 100644
--- a/crates/engine/src/scene_detect.rs
+++ b/crates/engine/src/scene_detect.rs
@@ -206,6 +206,7 @@ fn detect(s: &mut Session, p: &Value) -> Result<Value> {
*lock(&res) = Some(r);
prog.finished.store(true, Ordering::Relaxed);
};
+ let run = crate::export_tools::guard_job(job.progress.clone(), job.result.clone(), run);
s.jobs.push(job);
s.scene_jobs.push(PendingScene { job: id, seq: seq_id, apply, results: results.clone() });
let wait = bool_p(p, "wait").unwrap_or(false);