diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 2d0762268f..bdc3d07864 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -141,16 +141,27 @@ pub const SPOOLER_BUSY: &str = "the printer driver is busy with another print; t /// The GDI route's printers, or why they could not be listed. pub type RgbPrinters = std::result::Result, craft_print::rgb::PrintError>; -/// The last answers `system_printer_list` and `rgb_printers` got in this process. -static LAST_PRINTERS: std::sync::Mutex>> = std::sync::Mutex::new(None); -static LAST_RGB_PRINTERS: std::sync::Mutex> = std::sync::Mutex::new(None); +/// A list kept with the session's `printer_source` it came from (`None` for the system's printers). +type Kept = std::sync::Mutex, T)>>; -fn last(kept: &std::sync::Mutex>) -> Option { - kept.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clone() +/// The last answers `system_printer_list` and `rgb_printers` got in this process. +static LAST_PRINTERS: Kept> = std::sync::Mutex::new(None); +static LAST_RGB_PRINTERS: Kept = std::sync::Mutex::new(None); + +/// The kept list when it came from `source`; a list from other printers is not returned. +fn last(kept: &Kept, source: Option<&PrinterSource>) -> Option { + let kept = kept.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let (from, list) = kept.as_ref()?; + let same = match (from, source) { + (None, None) => true, + (Some(from), Some(source)) => std::sync::Arc::ptr_eq(from, source), + _ => false, + }; + same.then(|| list.clone()) } -fn keep(kept: &std::sync::Mutex>, value: T) { - *kept.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(value); +fn keep(kept: &Kept, source: Option<&PrinterSource>, value: T) { + *kept.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some((source.cloned(), value)); } /// Lists the printers; called with the spooler lock held. @@ -162,7 +173,7 @@ fn list_system_printers(s: &Session) -> Vec { craft_print::printers() } }; - keep(&LAST_PRINTERS, list.clone()); + keep(&LAST_PRINTERS, s.printer_source.as_ref(), list.clone()); list } @@ -181,7 +192,7 @@ fn list_rgb_printers(s: &Session) -> RgbPrinters { craft_print::rgb::printers() } }; - keep(&LAST_RGB_PRINTERS, list.clone()); + keep(&LAST_RGB_PRINTERS, s.printer_source.as_ref(), list.clone()); list } @@ -206,10 +217,11 @@ pub fn rgb_printers(s: &Session) -> RgbPrinters { } /// `rgb_printers` for the UI thread: while another thread lists printers or is in a printer driver -/// (a print job being handed to the spooler, Print Settings…), the list the last listing in this -/// process got, without waiting. Only the first listing in the process waits. +/// (a print job being handed to the spooler, Print Settings…), the list the last listing of the same +/// printers (the system's, or the same stand-in) got in this process, without waiting. Only the +/// first listing of those printers waits. pub fn rgb_printers_without_waiting(s: &Session) -> RgbPrinters { - try_with_spooler(|| list_rgb_printers(s)).or_else(|| last(&LAST_RGB_PRINTERS)).unwrap_or_else(|| rgb_printers(s)) + try_with_spooler(|| list_rgb_printers(s)).or_else(|| last(&LAST_RGB_PRINTERS, s.printer_source.as_ref())).unwrap_or_else(|| rgb_printers(s)) } /// `file.printers` for the UI thread, which lists the printers as `rgb_printers_without_waiting` @@ -218,7 +230,8 @@ pub fn printers_without_waiting(s: &mut Session) -> Value { if stand_in_printers(s.printer_source.as_ref(), cfg!(target_arch = "wasm32")).is_none() && system_spooler().is_none() { return system_printers(s, &json!({})).unwrap_or_default(); } - let list = try_with_spooler(|| list_system_printers(s)).or_else(|| last(&LAST_PRINTERS)).unwrap_or_else(|| system_printer_list(s)); + let list = + try_with_spooler(|| list_system_printers(s)).or_else(|| last(&LAST_PRINTERS, s.printer_source.as_ref())).unwrap_or_else(|| system_printer_list(s)); printers_answer(list) } diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index fc47a839ba..00137449b0 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -735,18 +735,18 @@ fn listings_without_waiting_return_the_last_lists_while_the_spooler_is_busy() { counted.fetch_add(1, Ordering::SeqCst); vec![craft_print::Printer { name: "Office".into(), default: true, two_sided: None }] })); - // Lists once, so there are last lists. - s.execute("file.printers", json!({})).unwrap(); - s.execute("file.print.printers", json!({})).unwrap(); let (held_tx, held) = mpsc::channel(); let (release, released_rx) = mpsc::channel::<()>(); let released = Arc::new(AtomicBool::new(false)); let holder = { let released = Arc::clone(&released); + let mut lister = Session::new(); + lister.printer_source = s.printer_source.clone(); std::thread::spawn(move || { with_spooler(|| { - // Nothing replaces the last lists while the lock is held. - held_tx.send((last(&LAST_PRINTERS), last(&LAST_RGB_PRINTERS))).unwrap(); + // Lists this stand-in's printers once with the lock held, so another test's listing + // cannot replace the last lists before the listings below. + held_tx.send((list_system_printers(&lister), list_rgb_printers(&lister))).unwrap(); // Bounded, so a listing that waits fails the test rather than hanging it. let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5)); released.store(true, Ordering::SeqCst); @@ -761,8 +761,40 @@ fn listings_without_waiting_return_the_last_lists_while_the_spooler_is_busy() { release.send(()).unwrap(); holder.join().unwrap(); assert_eq!(asked.load(Ordering::SeqCst), before, "a printer list was asked for while the spooler was busy"); - assert_eq!(listed, printers_answer(kept.unwrap())); - assert_eq!(listed_rgb, kept_rgb.unwrap()); + assert_eq!(listed, printers_answer(kept)); + assert_eq!(listed_rgb, kept_rgb); +} + +/// While the spooler is busy, a session whose printers were never listed waits for its own list: +/// the last lists another session's stand-in got are not its printers. +#[test] +fn listings_without_waiting_skip_the_lists_of_another_stand_in() { + use std::sync::{Arc, mpsc}; + let one = |name: &'static str| -> PrinterSource { Arc::new(move || vec![craft_print::Printer { name: name.into(), default: true, two_sided: None }]) }; + let mut other = Session::new(); + other.printer_source = Some(one("Elsewhere")); + other.execute("file.printers", json!({})).unwrap(); + other.execute("file.print.printers", json!({})).unwrap(); + let mut s = Session::new(); + s.printer_source = Some(one("Proofing")); + let (held_tx, held) = mpsc::channel(); + let holder = std::thread::spawn(move || { + with_spooler(|| { + held_tx.send(()).unwrap(); + std::thread::sleep(std::time::Duration::from_millis(500)); + }); + }); + held.recv().unwrap(); + let listed = printers_without_waiting(&mut s); + let listed_rgb = rgb_printers_without_waiting(&s); + holder.join().unwrap(); + assert_eq!(listed["printers"], json!([{"name": "Proofing", "default": true}])); + let names = listed_rgb.map(|list| list.into_iter().map(|p| p.name).collect::>()); + if cfg!(windows) { + assert_eq!(names, Ok(vec!["Proofing".to_string()])); + } else { + assert_eq!(names, Err(craft_print::rgb::PrintError::Unsupported)); + } } /// Spooling tests run in a child process: `disable_printing` is process-wide and a test above