mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Session::execute catches a command's panic and returns an ordinary Err (the last-resort guard in jobs.rs), so the unwind never reached panic_hunt's own catch_unwind and a panicking command counted as a pass. The harness now installs a panic hook that counts every panic in the process, including on rayon workers, and a run fails if the count moved. Runs are serialised so a panic is charged to the run that caused it. No production code changes. Test: contained_panics_count_and_graceful_errors_do_not. A contained panic fails the run and a graceful Err passes; the first assertion fails without the hook. The full --ignored fuzz stays green, so no command panics today. Co-authored-by: prekabreki <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 file changed
+48
-11
@@ -5,7 +5,8 @@
|
||||
use photocraft_engine::{Session, command_specs};
|
||||
use serde_json::{Value, json};
|
||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||
use std::sync::mpsc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Mutex, Once, PoisonError, mpsc};
|
||||
use std::time::Duration;
|
||||
|
||||
fn fresh() -> Session {
|
||||
@@ -22,19 +23,41 @@ fn fresh() -> Session {
|
||||
/// 0.3 s even on a heavily loaded machine, so this only catches real hangs.
|
||||
const DEADLINE: Duration = Duration::from_secs(4);
|
||||
|
||||
/// Run `id` with `p` on a fresh session, on a worker thread. `None` if it timed out, else whether
|
||||
/// it finished without panicking (returning Ok or a graceful Err both count).
|
||||
fn run_once(id: &str, p: &Value) -> Option<bool> {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let (id, p) = (id.to_string(), p.clone());
|
||||
std::thread::spawn(move || {
|
||||
let r = catch_unwind(AssertUnwindSafe(|| {
|
||||
let mut s = fresh();
|
||||
let _ = s.execute(&id, p);
|
||||
/// Panics anywhere in the process, counted by a hook. `Session::execute` contains a command's
|
||||
/// panic and returns an ordinary `Err`, so the unwind never reaches `catch_unwind` here; the hook
|
||||
/// still sees it, on whichever thread (rayon workers included) it happened (#4982).
|
||||
static PANICS: AtomicUsize = AtomicUsize::new(0);
|
||||
/// One run at a time, so a panic is charged to the run that caused it.
|
||||
static RUN: Mutex<()> = Mutex::new(());
|
||||
|
||||
/// Run `f` on a worker thread. `None` if it timed out, else whether it finished without
|
||||
/// panicking, contained or not (returning Ok or a graceful Err both count).
|
||||
fn run_watched(f: impl FnOnce() + Send + 'static) -> Option<bool> {
|
||||
static HOOK: Once = Once::new();
|
||||
HOOK.call_once(|| {
|
||||
let default = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |info| {
|
||||
PANICS.fetch_add(1, Ordering::SeqCst);
|
||||
default(info);
|
||||
}));
|
||||
});
|
||||
let _one = RUN.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let before = PANICS.load(Ordering::SeqCst);
|
||||
let (tx, rx) = mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let r = catch_unwind(AssertUnwindSafe(f));
|
||||
let _ = tx.send(r.is_ok());
|
||||
});
|
||||
rx.recv_timeout(DEADLINE).ok()
|
||||
rx.recv_timeout(DEADLINE).ok().map(|ok| ok && PANICS.load(Ordering::SeqCst) == before)
|
||||
}
|
||||
|
||||
/// Run `id` with `p` on a fresh session; see [`run_watched`].
|
||||
fn run_once(id: &str, p: &Value) -> Option<bool> {
|
||||
let (id, p) = (id.to_string(), p.clone());
|
||||
run_watched(move || {
|
||||
let mut s = fresh();
|
||||
let _ = s.execute(&id, p);
|
||||
})
|
||||
}
|
||||
|
||||
/// `Err` with a reason on panic or hang, `Ok` otherwise.
|
||||
@@ -80,3 +103,17 @@ fn no_command_panics_or_hangs_on_adversarial_params() {
|
||||
}
|
||||
assert!(bad.is_empty(), "{} commands panicked or hung:\n{}", bad.len(), bad.join("\n"));
|
||||
}
|
||||
|
||||
/// #4982: a panic the code under test contains still fails the run; a graceful `Err` does not.
|
||||
#[test]
|
||||
fn contained_panics_count_and_graceful_errors_do_not() {
|
||||
let contained = run_watched(|| {
|
||||
let _ = catch_unwind(|| panic!("contained, as Session::execute does"));
|
||||
});
|
||||
assert_eq!(contained, Some(false));
|
||||
let graceful = run_watched(|| {
|
||||
let mut s = fresh();
|
||||
assert!(s.execute("layer.delete", json!({"layer": 999999})).is_err());
|
||||
});
|
||||
assert_eq!(graceful, Some(true));
|
||||
}
|
||||
Reference in new issue
Block a user