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.
This commit is contained in:
dotCooCoo committed 2026-11-24 09:41:53 -08:00
1 parent 50dcc11e4d
commit 01ec822509
3 files changed
+70 -1

No files matched your search

+23 -1
View File
@@ -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.
+4
View File
@@ -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)
}
+43
View File
@@ -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]