From 01ec822509dfd2adde186cf25f543aed4c29ad9b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 09:41:53 -0800 Subject: [PATCH] Plug-ins: drop the open events of a script run that fails A script that opens a document and then throws, runs out of budget or is abandoned loses that document with its working copy, so its waiting document.opened event is now dropped when the run ends. The saves it made before failing are still delivered, and so is a document the frontend opened while the run was paused. --- crates/engine/src/plugin_events.rs | 24 ++++++++++++- crates/engine/src/script/mod.rs | 4 +++ crates/engine/src/tests/plugin_events.rs | 43 ++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/plugin_events.rs b/crates/engine/src/plugin_events.rs index 552d93bd21..555f2ac308 100644 --- a/crates/engine/src/plugin_events.rs +++ b/crates/engine/src/plugin_events.rs @@ -67,6 +67,9 @@ struct Pending { to: Vec<(Recipient, Vec<&'static str>)>, /// When it was queued ([`PluginEvents::queued`] then). seq: u64, + /// Queued by a command of the running script, whose run has not ended yet + /// ([`Session::plugin_events_script_ended`]). + by_script: bool, } impl Pending { @@ -169,7 +172,7 @@ impl PluginEvents { log::warn!("plug-in events: dropped a waiting {} event, past {MAX_PENDING} waiting", dropped.event.name()); } let to = to.into_iter().map(|r| (r, reasons.clone())).collect(); - self.pending.push_back(Pending { event, document: uid, to, seq: self.queued }); + self.pending.push_back(Pending { event, document: uid, to, seq: self.queued, by_script: false }); self.queued += 1; } @@ -270,6 +273,25 @@ impl Session { return; } self.plugin_events.queue(event, uid, Vec::new(), to); + // An open or a save is queued as its own entry, the newest. + if self.script_in_run + && let Some(p) = self.plugin_events.pending.back_mut() + { + p.by_script = true; + } + } + + /// At the end of a script's run. When the run failed (`kept` false), drops the waiting + /// `document.opened` events its commands queued, since the documents they opened were discarded + /// with its working copy. Its saves stay, since the files they wrote stay written, and so do the + /// events of documents a frontend opened while the run was paused. + pub(crate) fn plugin_events_script_ended(&mut self, kept: bool) { + if !kept { + self.plugin_events.pending.retain(|p| !(p.by_script && p.event == Event::DocumentOpened)); + } + for p in &mut self.plugin_events.pending { + p.by_script = false; + } } /// A mark for [`Session::forget_plugin_opens_since`]: the events queued so far. diff --git a/crates/engine/src/script/mod.rs b/crates/engine/src/script/mod.rs index 2d15652731..83f6b588d3 100644 --- a/crates/engine/src/script/mod.rs +++ b/crates/engine/src/script/mod.rs @@ -1910,6 +1910,8 @@ impl Session { lost }); drop(lost); + // The job's working copy went with it, with the documents the run opened. + self.plugin_events_script_ended(false); raise_ids(self); self.script_stop.clear(); self.reset_tool(); @@ -2182,6 +2184,7 @@ impl Session { let mut report = failure_report(&job.name, &e, file.as_deref(), &job.host, fuel_left, job.realm.limits().fuel.saturating_sub(fuel_left)); close_run(self, &job.host, &mut report, ending(&e.kind)); self.end_job(job, e.kind == RunErrorKind::Internal); + self.plugin_events_script_ended(false); raise_ids(self); self.script_stop.clear(); self.reset_tool(); @@ -2271,6 +2274,7 @@ impl Session { host.more_written(&mut report); close_run(self, host, &mut report, console::Ending::Finished); self.end_job(job, false); + self.plugin_events_script_ended(true); self.script_report = Some(report.clone()); Ok(report) } diff --git a/crates/engine/src/tests/plugin_events.rs b/crates/engine/src/tests/plugin_events.rs index 6ee0e8342b..9f757b020e 100644 --- a/crates/engine/src/tests/plugin_events.rs +++ b/crates/engine/src/tests/plugin_events.rs @@ -328,6 +328,49 @@ fn a_save_before_a_failed_batch_step_is_delivered() { run(&mut s, "plugin.remove", json!({"id": id})); } +/// A document a script opened before it failed is discarded with the run's working copy, so the +/// plug-in is not told of it. The save the script made before failing is still given, and so is a +/// document the frontend opened while the run was paused. +#[test] +fn a_failed_script_gives_its_save_but_not_its_open() { + let id = "test.events.failed_script"; + let mut s = session(); + let doc = s.doc().unwrap().uid; + install(&mut s, id, r#"["document.saved","document.opened"]"#, &[], "", tpl::LOG_EVENT, &[]); + s.plugin_event_frames = true; + let dir = std::env::temp_dir().join(format!("vectorcraft-engine-plugin-events-failed-script-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + run(&mut s, "document.save", json!({"path": dir.join("failed-script.vectorcraft").to_string_lossy()})); + s.deliver_plugin_events(); + assert_eq!(given(&mut s, id), [json!({"type": "document.saved", "document": doc})]); + let data = run(&mut s, "document.serialize", json!({}))["dataBase64"].as_str().unwrap().to_string(); + + s.script_frames = true; + let source = format!( + "vc.run('document.save', {{}}); vc.run('document.open', {{name: 'b.vectorcraft', dataBase64: '{data}'}}); \ + for (var i = 0; i < 3000000; i++) {{}} throw new Error('stop');" + ); + assert_eq!(run(&mut s, "script.run", json!({"source": source, "name": "t.js"}))["running"], true); + assert!(s.script_pump().is_none(), "paused after its first slice"); + let bytes = vectorcraft_format::base64_decode(&data).unwrap(); + let index = crate::cmd::fileio::open_bytes(&mut s, "front.vectorcraft", &bytes, None).unwrap()["index"].as_u64().unwrap(); + let front = s.documents()[index as usize].uid; + let mut slices = 0; + let r = loop { + slices += 1; + assert!(slices < 100_000, "the script ends"); + if let Some(r) = s.script_pump() { + break r; + } + }; + assert!(r.is_err(), "the script threw"); + assert!(s.docs.iter().all(|d| d.uid == doc || d.uid == front), "the script's document was discarded"); + s.deliver_plugin_events(); + assert_eq!(given(&mut s, id), [json!({"type": "document.saved", "document": doc}), json!({"type": "document.opened", "document": front})]); + std::fs::remove_dir_all(&dir).ok(); + run(&mut s, "plugin.remove", json!({"id": id})); +} + /// A `selection.changed` event of a plug-in without `doc.read` gives the count and no ids; one /// with it lists the selected ids. #[test]