From 92b4e9b9de264d1f002edf3079b3a97dd2b60e07 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 27 Oct 2026 00:11:47 -0700 Subject: [PATCH] Cancel pending link scans when closing a document Discard background scan results when a document closes so they cannot change the missing-link state after the same document reopens. --- crates/engine/src/link_watch.rs | 21 +++++++++++++++++++++ crates/engine/src/session/documents.rs | 1 + 2 files changed, 22 insertions(+) diff --git a/crates/engine/src/link_watch.rs b/crates/engine/src/link_watch.rs index 27d04bdce8..21f7007302 100644 --- a/crates/engine/src/link_watch.rs +++ b/crates/engine/src/link_watch.rs @@ -371,6 +371,27 @@ mod tests { assert_eq!(s.missing_links().len(), 1, "the earlier look found the file, but it is gone"); } + #[test] + fn a_scan_finished_after_close_cannot_change_a_reopened_document() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("art.png"); + let saved = directory.path().join("doc.pcraft"); + std::fs::write(&path, png(10, 10, "#ff0000")).unwrap(); + let (mut s, layer) = placed(&path); + s.execute("file.saveACopy", json!({"path": saved.to_string_lossy()})).unwrap(); + let id = s.active().unwrap().doc.id; + let stored = super::linked_files(&s.active().unwrap().doc)[0].0.clone(); + let scan = std::sync::Arc::new(std::sync::Mutex::new(None)); + s.link_scan = Some(scan.clone()); + s.close(0); + let bytes = std::fs::read(&saved).unwrap(); + crate::file_cmds::open_bytes_as(&mut s, "doc.pcraft", &bytes, None, Some(saved.to_string_lossy().into_owned())).unwrap(); + assert_eq!(s.active().unwrap().doc.id, id); + *scan.lock().unwrap() = Some((id.0, vec![(stored, vec![layer], None)])); + assert!(s.poll_link_scan().is_none()); + assert!(s.missing_links().is_empty()); + } + #[test] fn a_background_look_updates_changed_files() { let d = dir("scan"); diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index 0bed0c1d1b..3ae9685e12 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -141,6 +141,7 @@ impl Session { self.subject_cache.forget_doc(id); self.object_finder.forget_doc(id); self.link_missing.retain(|(d, _)| *d != id.0); + self.link_scan = None; } let d = self.docs.remove(index); self.active = if self.docs.is_empty() {