mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Plug-ins: keep the originating error across parallel bands (#1654)
When one band failed, its siblings stopped with a generic "another band failed" error, which could win the collection order and hide the real cause. The abort flag now carries the first error, so every band reports it; tests cover traps, error codes and the instruction budget across parallel bands. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c88dbd26cd
commit
40f14d25e8
4 files changed
+58
-13
No files matched your search
@@ -125,8 +125,8 @@ impl Plugin {
|
||||
let ctx = BandCtx { surface, extent, canvas, selection, fmt, ov, params: Value::Object(resolved) };
|
||||
let run = |b: &Rect| {
|
||||
let r = self.run_band(&ctx, *b, &deadline).map(|data| encode_band(surface, *b, &data));
|
||||
if r.is_err() {
|
||||
deadline.abort();
|
||||
if let Err(error) = &r {
|
||||
deadline.abort(error.clone());
|
||||
}
|
||||
r
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
//! The wasmi host: loading, instantiating and calling a plug-in under resource limits.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use wasmi::{
|
||||
Config, EnforcedLimits, Engine, Instance, Linker, Memory, Module, Store, StoreLimits, StoreLimitsBuilder, TypedFunc, TypedResumableCall, WasmParams,
|
||||
@@ -73,7 +72,7 @@ impl std::fmt::Debug for Plugin {
|
||||
pub(crate) struct Deadline {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
at: std::time::Instant,
|
||||
abort: Arc<AtomicBool>,
|
||||
abort: Arc<Mutex<Option<Error>>>,
|
||||
}
|
||||
|
||||
impl Deadline {
|
||||
@@ -83,20 +82,23 @@ impl Deadline {
|
||||
Self {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
at: std::time::Instant::now() + std::time::Duration::from_millis(limits.wall_time_ms),
|
||||
abort: Arc::new(AtomicBool::new(false)),
|
||||
abort: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
pub(crate) fn abort(&self) {
|
||||
self.abort.store(true, Ordering::Relaxed);
|
||||
pub(crate) fn abort(&self, error: Error) {
|
||||
// Publish the cause with the cancellation state so siblings cannot replace it with a
|
||||
// generic cancellation error, even when results are collected in a different order.
|
||||
self.abort.lock().unwrap_or_else(std::sync::PoisonError::into_inner).get_or_insert(error);
|
||||
}
|
||||
fn check(&self) -> Result<()> {
|
||||
if self.abort.load(Ordering::Relaxed) {
|
||||
return Err(Error::Failed("stopped because another band failed".into()));
|
||||
if let Some(error) = self.abort.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clone() {
|
||||
return Err(error);
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if std::time::Instant::now() >= self.at {
|
||||
self.abort();
|
||||
return Err(Error::Limit("time budget".into()));
|
||||
let error = Error::Limit("time budget".into());
|
||||
self.abort(error.clone());
|
||||
return Err(error);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -227,6 +227,47 @@ fn budgets_stop_runaway_plugins() {
|
||||
assert_eq!(e, Error::Limit("instruction budget".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parallel_bands_preserve_the_originating_error() {
|
||||
let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap();
|
||||
let mut s = Surface::new(PixelFormat::RGBA8);
|
||||
s.fill_rect(Rect::new(0, 0, 256, 256), &[0.0, 0.0, 0.0, 1.0]);
|
||||
s.fill_rect(Rect::new(0, 256, 256, 512), &[1.0, 0.0, 0.0, 1.0]);
|
||||
for (body, expected) in [
|
||||
("(i32.const 7)", Error::Failed("pc_filter returned error code 7".into())),
|
||||
("unreachable", Error::Trap("wasm `unreachable` instruction executed".into())),
|
||||
] {
|
||||
// The later band fails immediately while the earlier one is still running (or hasn't
|
||||
// started). Collecting in band order must not turn that failure into sibling cancellation.
|
||||
let filter = format!(
|
||||
"(if (result i32) (f32.gt (f32.load (local.get $buf)) (f32.const 0.5)) \
|
||||
(then {body}) (else (loop $spin (br $spin)) (i32.const 0)))"
|
||||
);
|
||||
let limits = Limits { band_bytes: 1, fuel_base: 100_000_000, fuel_per_sample: 0, ..Limits::default() };
|
||||
let p = Plugin::load(&ok_module(&filter), limits).unwrap();
|
||||
let before = s.clone();
|
||||
let e = pool.install(|| p.apply(&s, Rect::new(0, 0, 256, 512), None, &json!({}))).unwrap_err();
|
||||
assert_eq!(e, expected);
|
||||
assert_eq!(s, before, "a failed run must not change the input");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parallel_bands_preserve_instruction_budget() {
|
||||
let pool = rayon::ThreadPoolBuilder::new().num_threads(4).build().unwrap();
|
||||
let p = Plugin::load(
|
||||
&ok_module("(loop $spin (br $spin)) (i32.const 0)"),
|
||||
Limits { band_bytes: 1, fuel_base: 1_000_000, fuel_per_sample: 0, ..Limits::default() },
|
||||
)
|
||||
.unwrap();
|
||||
let mut s = Surface::new(PixelFormat::RGBA8);
|
||||
s.fill_rect(Rect::new(0, 0, 768, 512), &[0.25, 0.5, 0.75, 1.0]);
|
||||
for _ in 0..8 {
|
||||
let e = pool.install(|| p.apply(&s, Rect::new(0, 0, 768, 512), None, &json!({}))).unwrap_err();
|
||||
assert_eq!(e, Error::Limit("instruction budget".into()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wall_clock_budget_stops_runaway_plugins() {
|
||||
let spin = ok_module("(loop $l (br $l)) (i32.const 0)");
|
||||
|
||||
+3
-1
@@ -49,7 +49,9 @@ build. Limits (`photocraft_plugins::Limits`, defaults):
|
||||
Every band runs in a **fresh instance**, so no state survives between calls, and parallel bands
|
||||
never share memory. Traps, exhausted budgets, malformed modules, bad manifests, out-of-range
|
||||
pointers and non-zero return codes all become errors; the document is left untouched (the edit is
|
||||
transactional). Output samples that are NaN or infinite keep their original value; integer-depth
|
||||
transactional). When a failure stops sibling bands, those bands retain the originating error
|
||||
instead of replacing it with a generic cancellation message. Output samples that are NaN or
|
||||
infinite keep their original value; integer-depth
|
||||
documents and alpha are clamped to 0–1.
|
||||
|
||||
wasmi is built with its *portable* (loop) dispatcher: the default tail-call dispatcher relies on LLVM
|
||||
|
||||
Reference in new issue
Block a user