diff --git a/crates/analysis/src/rasterops.rs b/crates/analysis/src/rasterops.rs index 7f8fcbb2b5..26649c8a48 100644 --- a/crates/analysis/src/rasterops.rs +++ b/crates/analysis/src/rasterops.rs @@ -750,9 +750,17 @@ pub fn classify_min_distance(r: &Raster, bands: &[usize], means: &[(f32, Vec]) -> Option<(Vec, usize)> { - means_under(r, bands, rings, |_| Ok(())).unwrap_or(None) +/// The mean of each band over the pixels whose centres fall in a polygon (lon/lat rings). Its rows +/// count toward the run's progress, and a run cancelled under a control stops between rows +/// (#1562); without one it runs to the end. The rows are summed in order on one thread, so the +/// means are the same on every machine. +pub fn zonal_means(r: &Raster, bands: &[usize], rings: &[Vec]) -> Result, usize)>, crate::control::Cancelled> { + crate::control::begin(r.height as usize); + means_under(r, bands, rings, |rows| { + crate::control::check()?; + crate::control::advance(rows); + Ok(()) + }) } /// [`zonal_means`], `each_rows(n)` asked as the raster's rows go by, `n` at a time (each row @@ -1656,7 +1664,7 @@ mod tests { ]; for r in &rasters { for (z, rings) in zones.iter().enumerate() { - let got = zonal_means(r, &[0, 1], rings).map(|(m, n)| (m.iter().map(|v| v.to_bits()).collect::>(), n)); + let got = zonal_means(r, &[0, 1], rings).unwrap().map(|(m, n)| (m.iter().map(|v| v.to_bits()).collect::>(), n)); let want = every_cell(r, rings); assert!(want.as_ref().is_some_and(|(_, n)| *n > 100), "{} zone {z}: {want:?}", r.name); assert_eq!(got, want, "{} zone {z}", r.name); @@ -1670,12 +1678,16 @@ mod tests { let gt = GeoTransform { origin_x: top.x - 20_000.0, origin_y: top.y + 15_000.0, pixel_width: 100.0, pixel_height: -100.0 }; let r = Raster::new("polar", w, h, vec![band(0), band(1)], gt, polar, None, ColorHint::Gray, "Float32").unwrap(); let wide = vec![ring(&[(-61.0, -70.0), (59.0, -70.0), (59.0, -60.0), (-61.0, -60.0)])]; - let got = zonal_means(&r, &[0, 1], &wide).map(|(m, n)| (m.iter().map(|v| v.to_bits()).collect::>(), n)); + let got = zonal_means(&r, &[0, 1], &wide).unwrap().map(|(m, n)| (m.iter().map(|v| v.to_bits()).collect::>(), n)); let want = every_cell(&r, &wide); assert!(want.as_ref().is_some_and(|(_, n)| *n > 1000 && *n < (w * h) as usize), "{want:?}"); assert_eq!(got, want, "polar"); // A zone off the raster has none. - assert!(zonal_means(&rasters[0], &[0, 1], &[ring(&[(20.0, 10.0), (21.0, 10.0), (21.0, 11.0)])]).is_none()); + assert!(zonal_means(&rasters[0], &[0, 1], &[ring(&[(20.0, 10.0), (21.0, 10.0), (21.0, 11.0)])]).unwrap().is_none()); + // Under a control cancelled once it starts, a zone stops between rows (#1562). + let control = crate::control::Control::cancelled_once_started(); + let res = crate::run_under("Zone", &control, || zonal_means(&rasters[0], &[0, 1], &zones[0]).map_err(String::from)); + assert!(res.is_err() && control.looked() > 0, "{res:?}, looked {}", control.looked()); } /// The single passes the Analysis pane runs on a thread stop when their run is cancelled @@ -1716,7 +1728,7 @@ mod tests { // The class means equal the zonal means they are made of. let _none = crate::control::install(None); let c = class_means(&r, &[0, 1], &polygons[..1]).unwrap(); - assert_eq!(Some((c[0].1.clone(), c[0].2)), zonal_means(&r, &[0, 1], &polygons[0].1)); + assert_eq!(Some((c[0].1.clone(), c[0].2)), zonal_means(&r, &[0, 1], &polygons[0].1).unwrap()); } /// Band math and per-cell rows made on several threads give the same cells, bit for bit, as diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 5f21bb9a36..b15e086da9 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -623,8 +623,13 @@ pub(crate) enum Ahead { #[cfg(not(target_arch = "wasm32"))] /// What a model's or script's run on a copy of the session stops at, as its run here does them: a -/// tool that writes files (which would be written twice), plugin and GeoLibre tools, which run as -/// they always have, and a script's line that may reach outside the session. +/// tool that writes files (which would be written twice), a plugin tool (a plugin's program may do +/// more than hand back its layers, so it runs once, where the user ran it), and a script's line that may reach outside the session. A +/// GeoLibre tool runs ahead like a built-in one (#1562): the outputs it declares are written to a +/// scratch folder of its own and handed back, so the run here takes the result instead of running +/// it again, and Cancel stops its program. (The runner is trusted like any program the user +/// installs: a tool asked to write elsewhere through its parameters would, and would again if its +/// result isn't taken.) pub(crate) const NOT_AHEAD: &str = "runs where the model or script runs"; #[cfg(not(target_arch = "wasm32"))] @@ -2224,7 +2229,7 @@ pub(crate) fn execute( Some(ran) => ran, None => { #[cfg(not(target_arch = "wasm32"))] - if recording && (pin.resolved().source().is_some() || writes_files(spec)) { + if recording && (pin.plugin().is_some() || writes_files(spec)) { return Err(NOT_AHEAD.into()); } #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 555e6d3b8e..f4cba63874 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -920,6 +920,13 @@ fn spectral_points(s: &mut Session, r: &Raster, points: &Value) -> Result CmdResult { let r = raster(s, p)?; // A zone is read before points, and points before one point: what isn't read is said, once @@ -970,7 +977,7 @@ fn spectral_read(s: &mut Session, p: &Value, r: &Raster, zone: Option<&str>) -> return Err("the zone has no polygon".into()); }; let rings = polys.first().ok_or("the zone has no polygon")?; - let (means, n) = rasterops::zonal_means(r, &all, rings).ok_or("no pixels fall in the zone")?; + let (means, n) = rasterops::zonal_means(r, &all, rings)?.ok_or("no pixels fall in the zone")?; return answer(json!({"bands": names, "values": means, "pixels": n})); } let c = Coord::new(f(p, "lon").ok_or("give lon and lat, points or a zone")?, f(p, "lat").ok_or("lat is required")?); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f6af788625..05122df39f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -38222,3 +38222,23 @@ fn long_commands_stop_when_cancelled_while_they_run() { } assert!(kept_going.is_empty(), "these ran on after Cancel:\n{}", kept_going.join("\n")); } + +/// A GeoLibre step of a model is worked out on the copy and taken by the run here, as a built-in +/// tool's is (#1562): its program works in a scratch folder of its own, so running it ahead writes +/// nothing twice. Skipped without a GeoLibre runner (set RUSTGIS_GEOLIBRE). +#[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] +#[test] +fn a_models_geolibre_step_runs_ahead_and_is_taken() { + use crate::cmd::reads::{read_ahead, read_land}; + use rustgis_analysis::control::Control; + let mut s = with_raster(); + if s.run("gp.geolibre", &Value::Null).unwrap()["available"] != true { + eprintln!("skipped: no GeoLibre runner (set RUSTGIS_GEOLIBRE)"); + return; + } + s.run("model.new", &json!({"name": "Relief", "steps": [{"tool": "geolibre:slope", "params": {"input": "scene"}}]})).unwrap(); + let read = read_ahead(&mut s, "model.run", &json!({"name": "Relief"})).unwrap().run(&Control::stoppable()).unwrap(); + read_land(&mut s, read).unwrap(); + assert_eq!(s.tools_taken, 1, "the GeoLibre step was taken, not run again here"); + assert!(s.map().layer_by_name("Slope").is_some()); +} diff --git a/crates/geolibre/src/runner.rs b/crates/geolibre/src/runner.rs index d20f5e8d98..e0925b48b5 100644 --- a/crates/geolibre/src/runner.rs +++ b/crates/geolibre/src/runner.rs @@ -53,8 +53,15 @@ fn command(exe: &Path) -> Command { /// Runs `c` to completion, or kills it after `limit`. For the quick queries (version, /// manifests): a program that only happens to be called `geolibre` mustn't hang the app. Tool /// runs have no limit, since a large raster can legitimately take a long time. -fn output_within(mut c: Command, limit: Duration) -> Option { - let mut child = c.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().ok()?; +fn output_within(c: Command, limit: Duration) -> Option { + let start = Instant::now(); + output_until(c, &|| (start.elapsed() >= limit).then(|| "timed out".to_string())).ok() +} + +/// Runs `c` to completion, unless `stop` gives a reason first (looked at every 20 ms): then the +/// process is killed and the reason is the error. Both pipes are drained while it runs. +fn output_until(mut c: Command, stop: &dyn Fn() -> Option) -> Result { + let mut child = c.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().map_err(|e| e.to_string())?; // Drain both pipes on threads while the process runs: a full pipe (large manifests, or // many warnings) would otherwise block it until the time limit. fn drain(mut pipe: impl std::io::Read + Send + 'static) -> std::thread::JoinHandle> { @@ -68,24 +75,25 @@ fn output_within(mut c: Command, limit: Duration) -> Option { // Not expected with piped output, but never leave the process running. let _ = child.kill(); let _ = child.wait(); - return None; + return Err("the program's output couldn't be read".into()); }; let (out_reader, err_reader) = (drain(out), drain(err)); - let start = Instant::now(); let status = loop { - match child.try_wait() { + let why = match child.try_wait() { Ok(Some(status)) => break status, - Ok(None) if start.elapsed() < limit => std::thread::sleep(Duration::from_millis(20)), - _ => { - let _ = child.kill(); - let _ = child.wait(); - return None; - } + Ok(None) => stop(), + Err(e) => Some(e.to_string()), + }; + if let Some(why) = why { + let _ = child.kill(); + let _ = child.wait(); + return Err(why); } + std::thread::sleep(Duration::from_millis(20)); }; - let stdout = out_reader.join().ok()?; + let stdout = out_reader.join().map_err(|_| "the program's output couldn't be read".to_string())?; let stderr = err_reader.join().unwrap_or_default(); - Some(Output { status, stdout, stderr }) + Ok(Output { status, stdout, stderr }) } /// The runner's version, if `exe` is a GeoLibre runner (not some other program called geolibre). @@ -220,10 +228,11 @@ pub(crate) fn run_pinned( pin.check()?; let exe = &pin.exe; let mut c = command(exe); - c.current_dir(dir).stdin(Stdio::null()).arg(id).args(argv); + c.current_dir(dir).arg(id).args(argv); live()?; pin.check()?; - let out = c.output().map_err(|e| format!("{}: {e}", exe.display()))?; + let stop = || stop_reason(live); + let out = output_until(c, &stop).map_err(|e| if e == "cancelled" || e == CLOSED { e } else { format!("{}: {e}", exe.display()) })?; if !out.status.success() { let err = String::from_utf8_lossy(&out.stderr); let err = err.trim(); @@ -232,3 +241,68 @@ pub(crate) fn run_pinned( } Ok(String::from_utf8_lossy(&out.stdout).lines().map(str::to_string).collect()) } + +/// Why a program started for a project is stopped once another is open. +const CLOSED: &str = "its project was closed while the tool ran"; + +/// Why a running program is stopped rather than waited for (#1562): its run was cancelled (the +/// Cancel of a model or script run in the background), the project it was started for is no +/// longer the one open (the predicate a background read installs), or `live` fails. +fn stop_reason(live: &dyn Fn() -> Result<(), String>) -> Option { + if rustgis_analysis::control::current().is_some_and(|c| c.is_cancelled()) { + return Some("cancelled".to_string()); + } + if rustgis_data::source::remote::check_still(&rustgis_data::source::remote::current_still()).is_err() { + return Some(CLOSED.to_string()); + } + live().err() +} + +#[cfg(all(test, unix))] +mod tests { + use super::*; + + /// A program asked to stop is killed at once, not waited for (#1562): a GeoLibre step of a + /// model run in the background stops with its Cancel. + #[test] + fn a_program_asked_to_stop_is_killed() { + let mut c = Command::new("sleep"); + c.arg("30"); + let start = Instant::now(); + let e = output_until(c, &|| Some("cancelled".to_string())).unwrap_err(); + assert_eq!(e, "cancelled"); + assert!(start.elapsed() < Duration::from_secs(5), "{:?}", start.elapsed()); + // One that ends first gives its output. + let mut c = Command::new("echo"); + c.arg("done"); + let out = output_until(c, &|| None).unwrap(); + assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "done"); + } + + /// A program is stopped once its run is cancelled or its project closed, without a GeoLibre + /// runner: the path from the run's control and the installed predicate to the kill. + #[test] + fn a_cancelled_run_or_a_closed_project_stops_the_program() { + use rustgis_analysis::control::Control; + use rustgis_data::source::remote::with_still; + let fine = || Ok(()); + assert_eq!(stop_reason(&fine), None); + assert_eq!(stop_reason(&|| Err("gone".to_string())), Some("gone".to_string())); + let control = Control::new(); + let run = rustgis_analysis::run_under("Tool", &control, || { + assert_eq!(stop_reason(&fine), None); + control.cancel(); + let started = std::time::Instant::now(); + let mut c = Command::new("sleep"); + c.arg("30"); + let res = output_until(c, &|| stop_reason(&fine)); + assert!(started.elapsed() < Duration::from_secs(10), "the program was waited for"); + assert_eq!(res.err().as_deref(), Some("cancelled")); + Ok(()) + }); + assert!(run.is_err(), "the run ends cancelled"); + let closed = with_still(std::sync::Arc::new(|| false), || stop_reason(&fine)); + assert_eq!(closed.as_deref(), Some(CLOSED), "a program started for a project no longer open is stopped, and says so"); + assert_eq!(with_still(std::sync::Arc::new(|| true), || stop_reason(&fine)), None); + } +} diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index c06aa04430..305e4903ff 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1192,7 +1192,7 @@ same trust as installing that package. Tools that read files by path (LiDAR file since a web page has no file system. In the desktop app the Tools pane runs a GeoLibre tool in the background like any other; its -**Cancel** takes effect when the GeoLibre program ends, and the result is then dropped. On the +**Cancel** stops the GeoLibre program at once, and nothing is added. On the command line RustGIS waits while a tool runs. In the web app the run is started in the background and the result is added when it ends, but the tool itself runs in the page, so the page is busy while it computes. @@ -1288,7 +1288,7 @@ renames and definition queries, the environment and the projection between them, in the background first, so the map and the rest of the window stay in use: the status bar shows **Run Script** and **Cancel**, and the results say **Running…** until the run is added. From the first line of any other kind (an edit, Add Data, a save, an export, a style or a zoom, a plugin -or GeoLibre tool, a tool that writes a file) the rest runs as a script always has, holding the +tool, a tool that writes a file) the rest runs as a script always has, holding the window until it finishes. A tool whose inputs change meanwhile runs again as the results are added, as in a model's run. It colours the commands as you type, in both themes: `#` comments in green italics, the command id, and the JSON's strings, numbers and @@ -1365,8 +1365,11 @@ their name. **Running…** until the results are added, as one step to undo. What a tool reads that changes meanwhile (an edit, a selection, a layer added or reordered, the environment, the projection) has that tool and those after it run again as the results are added, and so does a tool that - writes a file, a plugin tool or a GeoLibre tool, with those after it. Those run as a model - always has, holding the window until they finish, with the work limits of a run that can't + writes a file or a plugin tool, with those after it. GeoLibre tools are worked out in the + background like the built-in ones: their outputs go to a folder of their own until they are + added. A GeoLibre option that names a file for the program to write isn't an output, so a + tool given one writes it in the background, and again if the tool runs again. Those that run again run as a model always has, holding the + window until they finish, with the work limits of a run that can't be cancelled. The tools worked out in the background take no work limit, as a run that can be stopped takes none. Tools that run holding the window or from the command line keep it, so the same model can finish in the background and stop at the limit there. A file a tool reads